If you're going to use this in many views, then you'd better implement a custom RazorPage:
public abstract class MyRazorPage<T> : RazorPage<T>
{
public async Task<bool> HasPolicyAsync(string policyName)
{
var authorizationService = Context.RequestServices.GetService(typeof(IAuthorizationService)) as IAuthorizationService;
bool isAdmin = (await authorizationService.AuthorizeAsync(User, policyName)).Succeeded;
return isAdmin;
}
}
then open _ViewImports.cshtml and add the next instruction:
@inherits MyRazorPage<TModel>
now you can call HasPolicyAsync() method from any view:
if (await HasPolicyAsync(Policies.RequireAdmin))
{
<h2>Admin is authorized</h2>
}
It would look much more concise.