-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
BaseAuthService.cs
42 lines (37 loc) · 1.1 KB
/
BaseAuthService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
namespace AuthServices;
using Microsoft.Identity.Client;
public abstract class BaseAuthService(IPublicClientApplication authenticationClient) : IAuthService
{
public Task<AuthenticationResult?> SignInInteractively(CancellationToken cancellationToken)
{
return authenticationClient
.AcquireTokenInteractive(Constants.Scopes)
.ExecuteAsync(cancellationToken);
}
public async Task<AuthenticationResult?> AcquireTokenSilent(CancellationToken cancellationToken)
{
try
{
var accounts = await authenticationClient.GetAccountsAsync(Constants.SignInPolicy);
var firstAccount = accounts.FirstOrDefault();
if (firstAccount is null)
{
return null;
}
return await authenticationClient.AcquireTokenSilent(Constants.Scopes, firstAccount)
.ExecuteAsync(cancellationToken);
}
catch (MsalUiRequiredException)
{
return null;
}
}
public async Task LogoutAsync(CancellationToken cancellationToken)
{
var accounts = await authenticationClient.GetAccountsAsync();
foreach (var account in accounts)
{
await authenticationClient.RemoveAsync(account);
}
}
}