[Authorize(Roles = "admin")] public class AdminController : Controller { }
The attribute also works on controllers.
You can even create a base controller and set an attribute on it (and therefore get the same authorization on all derived controllers)
[Authorize(Roles = "user")] public class BaseController : Controller { } public class NewsController : BaseController { } public class ForumController : BaseController { [HttpPost, Authorize(Roles="admin")] public ActionResult Delete(int id) { } }
Update
First question: you can put [HandleError] in your base controller to get MVC error handling in all controllers. I just wrote a blog post describing it.
Second question: Yes. Put the most specific [Authorize] attribute on the action. (for example, allow "users" in the base controller and "administrators" in the "Edit" action).
source share