Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattruma/8de8614d008e1a4a4583e0c778c36d8a to your computer and use it in GitHub Desktop.
Save mattruma/8de8614d008e1a4a4583e0c778c36d8a to your computer and use it in GitHub Desktop.
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