Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Roles Authorization

I want to make the roles default for my controller class to "Administrators, Content Editors"

[Authorize(Roles = "Administrators, Content Editor")]

I've done this by adorning the controller with the attribute above. However, there is one action that I want to be available to all (namely "View"). How can I reset the Roles so that everyone (including completely unauthorized users) have access for this action.

Note: I know I could adorn every single action other action with the authorize attribute above but I don't want to have to do that all the time. I want all of the controllers actions to be unacessible by default so that if anyone adds an action they have to make a considered decision to make it available to the general public.

like image 758
Mr Grok Avatar asked Apr 23 '09 07:04

Mr Grok


2 Answers

MVC4 has a new attribute exactly meant for this [AllowAnonymous]

[AllowAnonymous]
public ActionResult Register()

http://blogs.msdn.com/b/rickandy/archive/2012/03/23/securing-your-asp-net-mvc-4-app-and-the-new-allowanonymous-attribute.aspx

like image 138
Simon_Weaver Avatar answered Nov 18 '22 19:11

Simon_Weaver


You can place the Authorize attribute on the action methods. Not just at the class level.

So, move the attribute from the controller class to just the action methods you want to secure.

like image 29
Kieron Avatar answered Nov 18 '22 17:11

Kieron