Added Authorization
This commit is contained in:
parent
04049f31f6
commit
e47d6488c2
15 changed files with 134 additions and 21 deletions
|
@ -1,4 +1,7 @@
|
|||
@inherits LayoutComponentBase
|
||||
@using System.Security.Claims
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
@inject AuthenticationStateProvider AuthenticationStateProvider
|
||||
@inherits LayoutComponentBase
|
||||
|
||||
<PageTitle>Lieb</PageTitle>
|
||||
|
||||
|
@ -20,9 +23,54 @@
|
|||
</NotAuthorized>
|
||||
</AuthorizeView>
|
||||
</div>
|
||||
<AuthorizeView>
|
||||
<p>You can only see this if you are loged in</p>
|
||||
</AuthorizeView>
|
||||
<AuthorizeView Policy="Admin">
|
||||
<p>You can only see this if you satisfy the "Admin" policy.</p>
|
||||
</AuthorizeView>
|
||||
|
||||
|
||||
<article class="content px-4">
|
||||
@Body
|
||||
</article>
|
||||
<button @onclick="GetClaimsPrincipalData">Get ClaimsPrincipal Data</button>
|
||||
</main>
|
||||
</div>
|
||||
<p>@_authMessage</p>
|
||||
|
||||
@if (_claims.Count() > 0)
|
||||
{
|
||||
<ul>
|
||||
@foreach (var claim in _claims)
|
||||
{
|
||||
<li>@claim.Type: @claim.Value</li>
|
||||
}
|
||||
</ul>
|
||||
}
|
||||
|
||||
<p>@_surnameMessage</p>
|
||||
|
||||
@code {
|
||||
private string _authMessage;
|
||||
private string _surnameMessage;
|
||||
private IEnumerable<Claim> _claims = Enumerable.Empty<Claim>();
|
||||
|
||||
private async Task GetClaimsPrincipalData()
|
||||
{
|
||||
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
|
||||
var user = authState.User;
|
||||
|
||||
if (user.Identity.IsAuthenticated)
|
||||
{
|
||||
_authMessage = $"{user.Identity.Name} is authenticated.";
|
||||
_claims = user.Claims;
|
||||
_surnameMessage =
|
||||
$"Surname: {user.FindFirst(c => c.Type == ClaimTypes.Surname)?.Value}";
|
||||
}
|
||||
else
|
||||
{
|
||||
_authMessage = "The user is NOT authenticated.";
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue