Skip to content

Commit be1c28f

Browse files
committed
Avoid adding empty providers
1 parent 15513b1 commit be1c28f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

CommunityToolkit.Authentication.Uwp/WindowsProvider.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class WindowsProvider : BaseProvider
3333
private const string SettingsKeyProviderId = "WindowsProvider_ProviderId";
3434
private const string SettingsKeyProviderAuthority = "WindowsProvider_Authority";
3535

36-
private static readonly SemaphoreSlim SemaphoreSlim = new (1);
36+
private static readonly SemaphoreSlim SemaphoreSlim = new(1);
3737

3838
// Default/minimal scopes for authentication, if none are provided.
3939
private static readonly string[] DefaultScopes = { "User.Read" };
@@ -562,24 +562,37 @@ private async Task<List<WebAccountProvider>> GetWebAccountProvidersAsync()
562562
if (_webAccountProviderConfig.WebAccountProviderType == WebAccountProviderType.Any ||
563563
_webAccountProviderConfig.WebAccountProviderType == WebAccountProviderType.Msa)
564564
{
565-
providers.Add(await WebAuthenticationCoreManager.FindAccountProviderAsync(MicrosoftProviderId, MicrosoftAccountAuthority));
565+
await FindAndAddProviderAsync(MicrosoftProviderId, MicrosoftAccountAuthority);
566566
}
567567

568568
// AAD
569569
if (_webAccountProviderConfig.WebAccountProviderType == WebAccountProviderType.Any ||
570570
_webAccountProviderConfig.WebAccountProviderType == WebAccountProviderType.Aad)
571571
{
572-
providers.Add(await WebAuthenticationCoreManager.FindAccountProviderAsync(MicrosoftProviderId, AadAuthority));
572+
await FindAndAddProviderAsync(MicrosoftProviderId, AadAuthority);
573573
}
574574

575575
// Local
576576
if (_webAccountProviderConfig.WebAccountProviderType == WebAccountProviderType.Any ||
577577
_webAccountProviderConfig.WebAccountProviderType == WebAccountProviderType.Local)
578578
{
579-
providers.Add(await WebAuthenticationCoreManager.FindAccountProviderAsync(LocalProviderId));
579+
await FindAndAddProviderAsync(LocalProviderId);
580580
}
581581

582582
return providers;
583+
584+
async Task FindAndAddProviderAsync(
585+
string webAccountProviderId,
586+
string authority = default)
587+
{
588+
var provider = string.IsNullOrEmpty(authority)
589+
? await WebAuthenticationCoreManager.FindAccountProviderAsync(webAccountProviderId)
590+
: await WebAuthenticationCoreManager.FindAccountProviderAsync(webAccountProviderId, authority);
591+
if (provider != null)
592+
{
593+
providers.Add(provider);
594+
}
595+
}
583596
}
584597
}
585598
}

0 commit comments

Comments
 (0)