Reputation: 16290
Within a component we can use AuthorizeView
but what is the equivalent when we need to check if user is authorized in code?
Upvotes: 1
Views: 1615
Reputation: 11514
You can inject the AuthenticationStateProvider into a component as demonstrated in the docs.
This also works:
AuthenticationState
can be used. Wrap the <Router>
in a <CascadingAuthenticationState>
component and do something like this in your component:
@code {
[CascadingParameter]
private Task<AuthenticationState> authStateTask {get; set;}
protected override async Task OnInitializedAsync()
{
var authstate = await authStateTask;
var someRole = authstate.User.Claims.Single(c => c.Type == "your_role").Value;
//if role is OK do something....
}
}
Upvotes: 4