ASP.NET Core - Checking a user against a policy in Razor

Here is a small snippet of code that will allow you to check to see if a user is in a given policy (assuming you have already setup policies). In this case the code is setup to work from a Razor page (note the injection of the authorization service).


@inject IAuthorizationService auth

var isAuthorized = await auth.AuthorizeAsync(User, "Admin");

if (isAuthorized.Succeeded)
    // Do something

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.