ASP.NET MVC redirect to an access denied page using a custom role provider

后端 未结 9 1973
忘了有多久
忘了有多久 2020-12-12 11:20

I\'m creating a custom role provider and I set a Authorize attribute specifying a role in my controller and it\'s working just fine, like this:

[Authorize(Ro         


        
9条回答
  •  时光取名叫无心
    2020-12-12 11:37

    [AccessDeniedAuthorize(Roles="SuperAdmin")]
    public class SuperAdminController : Controller
    

    AccessDeniedAuthorizeAttribute.cs:

    public class AccessDeniedAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
    
            if(filterContext.Result is HttpUnauthorizedResult)
            {
                filterContext.Result = new RedirectResult("~/AcessDenied.aspx");
            }
        }
    }
    

提交回复
热议问题