I implemented Custom Policy=Based Authorization as per https://docs.asp.net/en/latest/security/authorization/policies.html.

This ended up being successful when it was accessed via the [Authorize] in a controller. However when I attempt to use it in my view as below. I get in issue with the context.Resource being null.

I am new to mvc so any help is appreciated.

View

@if (await AuthorizationService.AuthorizeAsync(User, "IsRegisteredUser"))
{
     <li><a asp-area="" asp-controller="Solution" asp-action="Index">Solutions</a></li>
}

Authorization Handler

protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, IsRegisteredUserRequirement requirement)
   {

            if(context.User.Identity.IsAuthenticated)
            {
                var mvcContext = context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext;


            }


        return Task.CompletedTask;
    }

Related posts

Recent Viewed