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?
It seems ASP.NET MVC 4 'fixed' this by adding an AllowAnonymous attribute.
David Hayden wrote about this :
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.
Then you can decorate your base controller with that attribute:
and for any controllers you don't want authorisation simply use the override with a 'false' - e.g.
My personal take on this would be to split the controller. Just create another controller For the actions you don't need authentication.
Or you could have :
BaseController
doesn't require authentication - here you have all your "base stuff" :).
BaseAuthController : BaseController
all actions here require authentication.
That way you can have authentication when you want , just by deriving from a specific class.
If you just want one action to be unauthorized on an otherwise authorized controller you can do something like this: