Authorize access to controllers depending on Locked property added to ApplicationUser

Jalza 736 Reputation points
2021-09-22T14:42:45.587+00:00

I'm migrating an ASP.NET MVC 5 project to ASP.NET Core MVC (.NET 5). In the previous project I had a
CustomAuthorizeAttribute
that checks if a user is locked o not (the first
else if (applicationUser.IsLocked)
statement).

public class CustomAuthorizeAttribute: AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        ActionResult actionResult = filterContext.Result;
        int statusCode = filterContext.HttpContext.Response.StatusCode;
        ApplicationUser applicationUser = filterContext.HttpContext.User.ApplicationUser();

        if (applicationUser == null || string.IsNullOrEmpty(filterContext.HttpContext.User.Identity.GetUserName()))
        {
            base.OnAuthorization(filterContext);
        }
        else if (applicationUser.IsLocked)
        {
            filterContext.Result = new RedirectResult("~/home/unauthorized");
        }
        // If they are authorized, handle accordingly
        else if (this.AuthorizeCore(filterContext.HttpContext))
        {
            base.OnAuthorization(filterContext);
        }
        else
        {
            // Otherwise redirect to your specific authorized area
            filterContext.Result = new RedirectResult("~/home/unauthorized");
        }
    }
}

It seems that this approach is not supported in ASP.NET Core, so I would like to know if it is possible to achieve this behavior and how can I develop it.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,131 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,621 Reputation points
    2021-09-22T15:15:56.337+00:00
    0 comments No comments