Is it possible to override the default behavior of [Authorize] in ASP.NET MVC?

前端 未结 5 1943
夕颜
夕颜 2020-12-16 03:05

I wondered if/how I can override the default [Authorize] behavior in ASP.NET MVC. I know that I can create a new Action Filter, make my own attribute and so forth; I am mere

5条回答
  •  情书的邮戳
    2020-12-16 03:42

    I see only 2 ways: overriding AuthorizeAttribute.OnAuthorization method or creating your own authorize attribute from scratch.

    1) very easy:

    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            base.OnAuthorization(filterContext);
    
            /// your behavior here
        }
    }
    

    2) easy too - just look at ASP.NET MVC source, AuthorizeAttribute.cs file

提交回复
热议问题