Currently I have [Authorize]
attributes on all of the methods on my AdminController
except for the Logon
action.
What's the cleanest way to invert this, so I don't have to remember to add the attributes to all methods, but rather add an attribute only to the method(s) that should be available without being logged in?
Would I be better just moving the Logon
action to its own controller, and applying the [Authorize]
attribute to the AdminController class?
You can do this using Filter Providers.
Phill Haack wrote about it here
Another way that you can do this with the
<location>
element in the web.config. Here's an example:In ASP.NET MVC 3 you could implement a custom global action filter provider:
which could be registered in
Application_Start
:Now if you are using some DI container such as NInject for example it supports filter binding syntax meaning that you could configure the kernel to inject the filter dynamically based on the context.
The pros of this approach is that now nomatter what controller or action is being added to your application => it will require authorization.
I would, as you suggested, move the Logon action to its own controller and apply the [Authorize] attribute to entire AdminController class. This is cleaner and will be easier to maintain in the future.
I usually do something like this:
And inherit from that w/a naming convention:
vs: