Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions CommunityToolkit.Authentication.Msal/MsalProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace CommunityToolkit.Authentication
/// </summary>
public class MsalProvider : BaseProvider
{
/// <inheritdoc />
public override string CurrentAccountId => _account?.HomeAccountId?.Identifier;

/// <summary>
/// Gets the MSAL.NET Client used to authenticate the user.
/// </summary>
Expand All @@ -26,6 +29,8 @@ public class MsalProvider : BaseProvider
/// </summary>
protected string[] Scopes { get; private set; }

private IAccount _account;

/// <summary>
/// Initializes a new instance of the <see cref="MsalProvider"/> class.
/// </summary>
Expand Down Expand Up @@ -62,9 +67,7 @@ public override async Task AuthenticateRequestAsync(HttpRequestMessage request)
/// <inheritdoc/>
public override async Task<bool> TrySilentSignInAsync()
{
var account = (await Client.GetAccountsAsync()).FirstOrDefault();

if (account != null && State == ProviderState.SignedIn)
if (_account != null && State == ProviderState.SignedIn)
{
return true;
}
Expand All @@ -85,8 +88,7 @@ public override async Task<bool> TrySilentSignInAsync()
/// <inheritdoc/>
public override async Task SignInAsync()
{
var account = (await Client.GetAccountsAsync()).FirstOrDefault();
if (account != null || State != ProviderState.SignedOut)
if (_account != null || State != ProviderState.SignedOut)
{
return;
}
Expand All @@ -107,10 +109,10 @@ public override async Task SignInAsync()
/// <inheritdoc />
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;
Expand All @@ -122,7 +124,7 @@ public override async Task<string> 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();
Expand All @@ -141,7 +143,7 @@ public override async Task<string> GetTokenAsync(bool silentOnly = false)
{
try
{
authResult = await Client.AcquireTokenInteractive(Scopes).ExecuteAsync();
authResult = await Client.AcquireTokenInteractive(Scopes).WithPrompt(Prompt.SelectAccount).ExecuteAsync();
}
catch
{
Expand All @@ -150,6 +152,8 @@ public override async Task<string> GetTokenAsync(bool silentOnly = false)
}
}

_account = authResult?.Account;

return authResult?.AccessToken;
}
}
Expand Down
3 changes: 3 additions & 0 deletions CommunityToolkit.Authentication.Uwp/WindowsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class WindowsProvider : BaseProvider
// The default account providers available in the AccountsSettingsPane.
private static readonly WebAccountProviderType DefaultWebAccountsProviderType = WebAccountProviderType.All;

/// <inheritdoc />
public override string CurrentAccountId => _webAccount?.Id;

/// <summary>
/// Gets the list of scopes to pre-authorize during authentication.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions CommunityToolkit.Authentication/BaseProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ protected set
}
}

/// <inheritdoc />
public abstract string CurrentAccountId { get; }

/// <inheritdoc/>
public event EventHandler<ProviderStateChangedEventArgs> StateChanged;

Expand Down
5 changes: 5 additions & 0 deletions CommunityToolkit.Authentication/IProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public interface IProvider
/// </summary>
ProviderState State { get; }

/// <summary>
/// Gets the id of the currently signed in user account.
/// </summary>
string CurrentAccountId { get; }

/// <summary>
/// Event called when the login <see cref="State"/> changes.
/// </summary>
Expand Down
3 changes: 3 additions & 0 deletions CommunityToolkit.Authentication/MockProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public MockProvider(bool signedIn = true)
State = signedIn ? ProviderState.SignedIn : ProviderState.SignedOut;
}

/// <inheritdoc />
public override string CurrentAccountId => State == ProviderState.SignedIn ? "mock-account-id" : null;

/// <inheritdoc/>
public override Task AuthenticateRequestAsync(HttpRequestMessage request)
{
Expand Down