asp.net mvc 4 - AuthorizeRoles=admin how redirect to index when is false. -
in mvc 4 application simplemembershipprovider
. i'm decorating controller attribute [authorize(roles = "admin")]
.
when roles false i'm redirecting user login page. how can change redirect index page?
you can use custom authorize attribute overriding authorizeattribute
as
[customauthattribute(roles = "admin")]
code:
using system; using system.web.http; using system.net.http; public class customauthattribute : authorizeattribute { public override void onauthorization(system.web.http.controllers.httpactioncontext actioncontext) { /* if authorization condition fails */ if(!condition) { handleunauthorizedrequest(actioncontext); } } protected override void handleunauthorizedrequest(system.web.http.controllers.httpactioncontext actioncontext) { filtercontext.result = new redirecttorouteresult( new routevaluedictionary { { "action", "/*actionname*/" }, { "controller", "/*controllername*/" } }); } }
Comments
Post a Comment