-
Notifications
You must be signed in to change notification settings - Fork 16
/
Claims.razor
66 lines (57 loc) · 1.89 KB
/
Claims.razor
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
@page "/claims"
@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization
@inject HttpClient HttpClient
@inject NavigationManager Navigation
@inject AuthenticationStateProvider AuthenticationStateProvider
<div>
<h3>Claims</h3>
<p>List of claims for the current user.</p>
</div>
<AuthorizeView Roles="Users, Administrators">
<Authorized>
<div>
<p>Hello, @context.User.Identity.Name!</p>
@if (_claims.Count() > 0)
{
<ul>
@foreach (var claim in _claims)
{
<li>@claim.Type – @claim.Value</li>
}
</ul>
}
</div>
</Authorized>
<NotAuthorized>
<div class="w-75 p-3">
<p>You'll need to <a href="" @onclick="IdentityLogin" @onclick:preventDefault><b>login</b></a> as a <b>User</b> (<i>email: <b>[email protected]</b> password: <b>Qwerty1234#</b></i>) or an <b>Administrator</b> (<i>email: <b>[email protected]</b> password: <b>Qwerty1234#</b></i>) to view this component.</p>
</div>
</NotAuthorized>
</AuthorizeView>
<div>
<p>@_authMessage</p>
</div>
@code
{
private string _authMessage;
private IEnumerable<Claim> _claims = Enumerable.Empty<Claim>();
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
_authMessage = $"{user.Identity.Name} is authenticated.";
_claims = user.Claims;
}
else
{
_authMessage = "The user is NOT authenticated.";
}
}
private void IdentityLogin()
{
Navigation.NavigateTo($"authentication/login?returnUrl=" + Uri.EscapeDataString(Navigation.Uri));
}
}