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).

C#

@inject IAuthorizationService auth

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

if (isAuthorized.Succeeded)
{
    // Do something
}