-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathMyTargetingContextAccessor.cs
More file actions
39 lines (30 loc) · 1.21 KB
/
MyTargetingContextAccessor.cs
File metadata and controls
39 lines (30 loc) · 1.21 KB
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
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.FeatureManagement.FeatureFilters;
namespace BlazorServerApp
{
public class MyTargetingContextAccessor : ITargetingContextAccessor
{
private readonly AuthenticationStateProvider _authenticationStateProvider;
public MyTargetingContextAccessor(AuthenticationStateProvider authenticationStateProvider)
{
_authenticationStateProvider = authenticationStateProvider ?? throw new ArgumentNullException(nameof(authenticationStateProvider));
}
public async ValueTask<TargetingContext> GetContextAsync()
{
AuthenticationState authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
string username = authState.User.Identity.Name;
bool isAuthenticated = authState.User.Identity.IsAuthenticated;
var groups = new List<string>();
if (!isAuthenticated)
{
groups.Add("Guests");
}
var targetingContext = new TargetingContext
{
UserId = username,
Groups = groups
};
return targetingContext;
}
}
}