Skip to content

Instantly share code, notes, and snippets.

@vpetkovic
Last active July 28, 2020 20:22
Show Gist options
  • Save vpetkovic/b60c3d9908f0560896222f2b04a1758c to your computer and use it in GitHub Desktop.
Save vpetkovic/b60c3d9908f0560896222f2b04a1758c to your computer and use it in GitHub Desktop.
This is just a boilerplate to save a bit of time with copy paste when needed
@page "/Claims"
@inject AuthenticationStateProvider AuthState
<AuthorizeView>
<Authorized>
<h2>
Hello @Username,
here's the list of your claims:
</h2>
<ul>
@foreach (var claim in context.User.Claims)
{
<li><b>@claim.Type</b>: @claim.Value</li>
}
</ul>
</Authorized>
<NotAuthorized>
<p>I'm sorry, I can't display anything until you log in</p>
</NotAuthorized>
</AuthorizeView>
@code
{
[CascadingParameter] Task<AuthenticationState> authenticationStateTask { get; set; }
private string Username = "Anonymous User";
protected override async Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var user = authState.User;
Username = user.Claims
.Where(c => c.Type.Equals("[Claim that represents user's name in any context]"))
.Select(c => c.Value)
.FirstOrDefault() ?? string.Empty;
await base.OnInitializedAsync();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment