c# - ASP.NET MVC 4 User Authentication -
i trying write login method authenticates , authorizes users web site developed asp.net mvc 4. problem is, although call formsauthentication.setauthcookie method after validating user inside login method , redirect viewprofile action, user.identity.isauthenticated returns still false in custom authorize attribute object.
i gave code below:
[httppost] [allowanonymous] public actionresult login(loginmodel model) { if (membership.validateuser(model.username, model.password)) { formsauthentication.setauthcookie(model.username, model.rememberme); return this.redirecttoaction("viewprofile"); } modelstate.addmodelerror("", "the user name or password provided incorrect."); return view(model); } [myauthorize(roles = "super role, seeker")] public actionresult viewprofile() { if (modelstate.isvalid) { ... } }
and here code of myauthorizeattribute class
[attributeusage(attributetargets.method, allowmultiple = false)] public class myauthorizeattribute : authorizeattribute { protected override bool authorizecore(httpcontextbase httpcontext) { if (httpcontext == null) throw new argumentnullexception("httpcontext"); var user = httpcontext.user; if (!user.identity.isauthenticated) return false; ... } }
what missing?
for ones may use formsauthentication , face similar situation, sure following configuration exists under web.config file
<authentication mode="forms"></authentication>
now works fine
Comments
Post a Comment