i have a project using ASP.Net MVC3 and using membership for roles. i use authorize in every controller. eg:
[Authorize(Roles = "Administrator")]
public ActionResult Index(string q, int i)
{
return View(model);
}
if someone doesnt have role for administrator, then it will redirect to login page by default. how to change it,so it will redirect into Views/Shared/UnAuthorize.cshtml ? or maybe if someone doesnt have role for administrator, it will show message box (alert) ?
thanks in advance.
i solved my problem. i only do this :
and apply MyAuthorize to class or action:
thats it.
I use this method and it is very easy to implement.
Securing Asp.net MVC3
Change your default route to logon page in global.asax
My own version, based on ntep vodka's:
This way I get standard redirect to login page for not authenticated users, and custom redirect for users that are authenticated but don't have the appropriate role for the action.
Just change the page that have to be shown in the web.config (check that the route exists)
If you, instead, want to redirect to a specific path for every roles you can extend the AuthorizeAttribute with your own. Something like this (not tested, I write this to give you an idea)
The code below helped and here is the reference in stackoverflow ASP.NET MVC 4 custom Authorize attribute - How to redirect unauthorized users to error page?
Well, you can inherit from
AuthorizeAttribute
and overrideHandleUnauthorizedRequest
which is responsible for redirection of unauhorized/unauthenticated requests. i think this question will be helpful to you