asp.net mvc - Register _LoginPartial if(Request.IsAuthenticated) -
i'm hitting dead end one. i'm new mvc , see similiar issue appearing on so, not help. i'm trying set register action, , once user registered _loginpartial view should indicate user authenticated. various posts , articles read believe i'm missing element of storing user cookie, not know how achieve this. appreciate hints.
controler:
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult register(registermodel model) websecurity.createuserandaccount(model.username, model.password); int userid = websecurity.getuserid(model.username); string rolename = configurationmanager.appsettings["customerrole"]; if (!roles.roleexists(rolename)){ roles.createrole(rolename); } roles.addusertorole(model.username, rolename); var customer = new customer(); customer.userid = userid; customer.name = model.name; customer.privateemail = model.privateemail; _customerrepo.add(customer); _customerrepo.savechanges(); // customer, membership, userprofile added db, no problems tempdata["message"] = "user registered"; // shows return redirecttoaction("index", "home"); // shows
it seems being saved correctly partial view not see user anymore... _loginpartial view
@if(request.isauthenticated) { <text> user: @html.actionlink(user.identity.name, "manage", "account",routevalues: null, htmlattributes: new { @class = "username", title = "change password" }) @using (html.beginform("logoff", "account", formmethod.post, new { id = "logoutform" })) {@html.antiforgerytoken() <a href="javascript:document.getelementbyid('logoutform').submit()">log off</a>} </text> } else { <ul> <li>@html.actionlink("register", "register", "account", routevalues: null, htmlattributes: new { id = "registerlink" })</li> <li>@html.actionlink("register", "login", "account", routevalues: null, htmlattributes: new { id = "loginlink" })</li> </ul> }
you need call login() after savechanges();
it should like
websecurity.login(model.username, model.password, false);
more information http://www.codeguru.com/csharp/.net/net_asp/mvc/using-simplemembership-in-asp.net-mvc-4.htm
Comments
Post a Comment