question

jalza avatar image
0 Votes"
jalza asked Bruce-SqlWork answered

Authorize access to controllers depending on Locked property added to ApplicationUser

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.

dotnet-aspnet-core-mvcdotnet-aspnet-core-general
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.