asp.net - AuthorizeAttribute Redirect After user Login -
i using default web application template mvc4 in visual studio. how can write authorize attribute redirect user after succesfull login? attribute should this: [userredirect("username")] whether use "username" login redirect should made. apply attribute? login page or index? answer.
you need override onauthorization method of authorizeatribute
[attributeusage(attributetargets.method | attributetargets.class, inherited = true, allowmultiple = true)] public class userredirectattribute: authorizeattribute { public string username{ get; set; } public override void onauthorization(authorizationcontext filtercontext) { base.onauthorization(filtercontext); if(base.authorizecore(filtercontext.httpcontext) == true) { //here put logic redirect condition () if(filtercontext.httpcontext.user.identity.name.equals(this.username,stringcomparer.ordinal) { //if should redirect filtercontext.result = new redirecttorouteresult( new routevaluedictionary( new { controller = "redirect controller name", action = "redirect action name" }) ); } } } } regarding usage:
if authorize attribute going replace default authorize attribute should on authorized action/controller
edit:
based on last comment don't have use attribute @ all. add redirect logic @ end of login action:
[httppost] public actionresult login(loginmodel model) { bool userauthenticationresult = // here goes authentication logic if(userauthenticationresult && httpcontext.user.identity.name.equals(this.username,stringcomparer.ordinal)) { //setup cookie/token other things need authentication work return redirecttoaction("actionname","controllername"); } }
Comments
Post a Comment