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

前端 未结 5 1942
夕颜
夕颜 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条回答
  •  旧时难觅i
    2020-12-16 03:24

    Yes, take a look at the MSDN docs for AuthorizeAttribute: http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx.

    Basically, you can override the OnAuthorization() method and customize the behavior. There are other virtual methods on the attribute as well.

    EDIT: As Bruno pointed out, you can override the AuthorizeCore() method. The main difference being that AuthorizeCore() takes an HttpContextBase, while OnAuthorization() takes an AuthorizationContext. An instance of AuthorizationContext provides you with more information, such as the Controller, the RequestContext and the RouteData. It also lets you specify an ActionResult.

    AuthorizeCore() is more restricted in the information you can access as well as the result you can return, but if you need to authorize cached data, then your logic needs to handle the case where you don't have any of that extra data (since data is served from the cache before the request is routed through the MVC pipeline).

    As always, you need to understand your scenario and the available tools and trade-offs between them.

提交回复
热议问题