diff --git a/CommunityToolkit.Authentication.Msal/MsalProvider.cs b/CommunityToolkit.Authentication.Msal/MsalProvider.cs
index b01c70a..b871901 100644
--- a/CommunityToolkit.Authentication.Msal/MsalProvider.cs
+++ b/CommunityToolkit.Authentication.Msal/MsalProvider.cs
@@ -16,6 +16,9 @@ namespace CommunityToolkit.Authentication
///
public class MsalProvider : BaseProvider
{
+ ///
+ public override string CurrentAccountId => _account?.HomeAccountId?.Identifier;
+
///
/// Gets the MSAL.NET Client used to authenticate the user.
///
@@ -26,6 +29,8 @@ public class MsalProvider : BaseProvider
///
protected string[] Scopes { get; private set; }
+ private IAccount _account;
+
///
/// Initializes a new instance of the class.
///
@@ -62,9 +67,7 @@ public override async Task AuthenticateRequestAsync(HttpRequestMessage request)
///
public override async Task TrySilentSignInAsync()
{
- var account = (await Client.GetAccountsAsync()).FirstOrDefault();
-
- if (account != null && State == ProviderState.SignedIn)
+ if (_account != null && State == ProviderState.SignedIn)
{
return true;
}
@@ -85,8 +88,7 @@ public override async Task TrySilentSignInAsync()
///
public override async Task SignInAsync()
{
- var account = (await Client.GetAccountsAsync()).FirstOrDefault();
- if (account != null || State != ProviderState.SignedOut)
+ if (_account != null || State != ProviderState.SignedOut)
{
return;
}
@@ -107,10 +109,10 @@ public override async Task SignInAsync()
///
public override async Task SignOutAsync()
{
- // Forcibly remove each user.
- foreach (var user in await Client.GetAccountsAsync())
+ if (_account != null)
{
- await Client.RemoveAsync(user);
+ await Client.RemoveAsync(_account);
+ _account = null;
}
State = ProviderState.SignedOut;
@@ -122,7 +124,7 @@ public override async Task GetTokenAsync(bool silentOnly = false)
AuthenticationResult authResult = null;
try
{
- var account = (await Client.GetAccountsAsync()).FirstOrDefault();
+ var account = _account ?? (await Client.GetAccountsAsync()).FirstOrDefault();
if (account != null)
{
authResult = await Client.AcquireTokenSilent(Scopes, account).ExecuteAsync();
@@ -141,7 +143,7 @@ public override async Task GetTokenAsync(bool silentOnly = false)
{
try
{
- authResult = await Client.AcquireTokenInteractive(Scopes).ExecuteAsync();
+ authResult = await Client.AcquireTokenInteractive(Scopes).WithPrompt(Prompt.SelectAccount).ExecuteAsync();
}
catch
{
@@ -150,6 +152,8 @@ public override async Task GetTokenAsync(bool silentOnly = false)
}
}
+ _account = authResult?.Account;
+
return authResult?.AccessToken;
}
}
diff --git a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
index d21f319..e5ca726 100644
--- a/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
+++ b/CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
@@ -41,6 +41,9 @@ public class WindowsProvider : BaseProvider
// The default account providers available in the AccountsSettingsPane.
private static readonly WebAccountProviderType DefaultWebAccountsProviderType = WebAccountProviderType.All;
+ ///
+ public override string CurrentAccountId => _webAccount?.Id;
+
///
/// Gets the list of scopes to pre-authorize during authentication.
///
diff --git a/CommunityToolkit.Authentication/BaseProvider.cs b/CommunityToolkit.Authentication/BaseProvider.cs
index f6b98da..3f7a24e 100644
--- a/CommunityToolkit.Authentication/BaseProvider.cs
+++ b/CommunityToolkit.Authentication/BaseProvider.cs
@@ -34,6 +34,9 @@ protected set
}
}
+ ///
+ public abstract string CurrentAccountId { get; }
+
///
public event EventHandler StateChanged;
diff --git a/CommunityToolkit.Authentication/IProvider.cs b/CommunityToolkit.Authentication/IProvider.cs
index 714de0a..cd065a2 100644
--- a/CommunityToolkit.Authentication/IProvider.cs
+++ b/CommunityToolkit.Authentication/IProvider.cs
@@ -18,6 +18,11 @@ public interface IProvider
///
ProviderState State { get; }
+ ///
+ /// Gets the id of the currently signed in user account.
+ ///
+ string CurrentAccountId { get; }
+
///
/// Event called when the login changes.
///
diff --git a/CommunityToolkit.Authentication/MockProvider.cs b/CommunityToolkit.Authentication/MockProvider.cs
index 4fbd608..c3b5d99 100644
--- a/CommunityToolkit.Authentication/MockProvider.cs
+++ b/CommunityToolkit.Authentication/MockProvider.cs
@@ -26,6 +26,9 @@ public MockProvider(bool signedIn = true)
State = signedIn ? ProviderState.SignedIn : ProviderState.SignedOut;
}
+ ///
+ public override string CurrentAccountId => State == ProviderState.SignedIn ? "mock-account-id" : null;
+
///
public override Task AuthenticateRequestAsync(HttpRequestMessage request)
{