Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mattruma.com
@page "/profile"
@using System.Security.Claims
@using Microsoft.AspNetCore.Components.Authorization
<section class="mb-4">
<h1 class="mb-4">Authorize View Component</h1>
<AuthorizeView>
<Authorized>
<h2 class="mb-4">Hello, @context.User.Identity.Name!</h2>
<table class="table">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
@foreach (var claim in context.User.Claims)
{
<tr>
<td>@claim.Type</td>
<td>@claim.Value</td>
</tr>
}
</tbody>
</table>
</Authorized>
</AuthorizeView>
</section>
<section class="mb-4">
<h1 class="mb-4">Authentication State</h1>
<h2 class="mb-4">Hello, @this.AuthenticationStateUser.Identity.Name!</h2>
<table class="table">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
@foreach (var claim in this.AuthenticationStateUser.Claims)
{
<tr>
<td>@claim.Type</td>
<td>@claim.Value</td>
</tr>
}
</tbody>
</table>
</section>
@inject AuthenticationStateProvider AuthenticationStateProvider
<section class="mb-4">
<h1 class="mb-4">Authentication State Provider</h1>
<h2 class="mb-4">Hello, @this.AuthenticationStateProviderUser.Identity.Name!</h2>
<table class="table">
<thead>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
</thead>
<tbody>
@foreach (var claim in this.AuthenticationStateProviderUser.Claims)
{
<tr>
<td>@claim.Type</td>
<td>@claim.Value</td>
</tr>
}
</tbody>
</table>
</section>
@code {
[CascadingParameter]
private Task<AuthenticationState> authenticationStateTask { get; set; }
private ClaimsPrincipal AuthenticationStateUser { get; set; }
private ClaimsPrincipal AuthenticationStateProviderUser { get; set; }
protected override async Task OnParametersSetAsync()
{
AuthenticationState authenticationState;
authenticationState = await authenticationStateTask;
this.AuthenticationStateUser = authenticationState.User;
authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
this.AuthenticationStateProviderUser = authenticationState.User;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.