Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Authorize Attribute in ASP.NET MVC

I have an MVC controller base class on which I applied the Authorize attribute since I want almost all of the controllers (and their actions along) to be authorized.

However I need to have a controller and an action of another controller unauthorized. I wanted to be able to decorate them with the [Authorize(false)] or something but this is not available.

Any ideas?

like image 904
Andrei Rînea Avatar asked Apr 14 '09 10:04

Andrei Rînea


People also ask

How do I override an authorized attribute?

You could create a custom authorisation attribute inheriting from the standard AuthorizeAttribute with an optional bool parameter to specify whether authorisation is required or not. and for any controllers you don't want authorisation simply use the override with a 'false' - e.g.

How can use Authorize attribute in ASP NET MVC?

Here's how to use the Authorize attribute. You can apply the Authorize attribute to individual methods as well as the controller class as a whole. If you add the Authorize attribute to the controller class, then any action methods on the controller will be only available to authenticated users.

How do I override OnAuthorization in .NET core?

We have code base ready, we need to implement the wrapper class to handle the API request. Right-click on the solution and add a new class. Enter the class name and click on Add. Next Inherite Attribute, IAuthorizationFilter to CustomAuthorization class which has overridden the OnAuthorization method.


2 Answers

Edit: Since ASP.NET MVC 4 the best approach is simply to use the built-in AllowAnonymous attribute.

The answer below refers to earlier versions of ASP.NET MVC

You could create a custom authorisation attribute inheriting from the standard AuthorizeAttribute with an optional bool parameter to specify whether authorisation is required or not.

public class OptionalAuthorizeAttribute : AuthorizeAttribute {     private readonly bool _authorize;      public OptionalAuthorizeAttribute()     {         _authorize = true;     }      public OptionalAuthorizeAttribute(bool authorize)     {         _authorize = authorize;     }      protected override bool AuthorizeCore(HttpContextBase httpContext)     {         if(!_authorize)             return true;                      return base.AuthorizeCore(httpContext);     } } 

Then you can decorate your base controller with that attribute:

[OptionalAuthorize] public class ControllerBase : Controller { } 

and for any controllers you don't want authorisation simply use the override with a 'false' - e.g.

[OptionalAuthorize(false)] public class TestController : ControllerBase {     public ActionResult Index()     {         return View();     } } 
like image 184
Steve Willcock Avatar answered Nov 08 '22 16:11

Steve Willcock


It seems ASP.NET MVC 4 'fixed' this by adding an AllowAnonymous attribute.

David Hayden wrote about this :

[Authorize] public class AccountController : Controller {     [AllowAnonymous]     public ActionResult Login()     {         // ...     }      // ... } 
like image 24
Andrei Rînea Avatar answered Nov 08 '22 16:11

Andrei Rînea