asp.net mvc - C# MVC 5 The ticket cookie is cleared when the form authentication signs out -
i need access cookies user , password , set them in text boxes of login view because in view checked "remember me".
logoff method
public actionresult logoff() { //session.abandon(); // sign out. formsauthentication.signout(); return redirecttoaction("index", "login"); }
initialization of sessions , cookies after successful login.
private void initializesessionvariables(agentdto user) { // sessionmodel.agentid = user.id; response.cookies.clear(); formsauthenticationticket ticket = new formsauthenticationticket(1,user.mobilephone,datetime.now,datetime.now.adddays(30),true,"",formsauthentication.formscookiepath); // encrypt ticket. string encryptedticket = formsauthentication.encrypt(ticket); // create cookie. httpcookie authenticationcookie = new httpcookie(formsauthentication.formscookiename, encryptedticket); // name of auth cookie (it's name specified in web.config) // hashed ticket authenticationcookie.expires = datetime.now.adddays(365); // add cookie list outbound response response.cookies.add(authenticationcookie); }
action result of login view have problem when first log out , try access cookie returns null because run "formsauthentication.signout ();"
public actionresult index(logondto model, string message = null, string reason = null) { if (sessionmodel.agentmobilephone != null) return redirecttoaction("index", "home"); if (reason != null) message = "su sessiĆ³n ha expirado. vuelva loguearse."; viewbag.message = message; if (request.cookies[formsauthentication.formscookiename] != null) { httpcookie authcookie = request.cookies[formsauthentication.formscookiename]; formsauthenticationticket authticket = formsauthentication.decrypt(authcookie.value); model.username = authticket.name; //model.password = "in progress..." } return view(model); }
you can use javascript store user information if click on remember me checkbox
use
localstorage.setitem("username", "smith");
to set values
and on login page on document ready event of jquery write below code
var username = localstorage.getitem("username"); if (username) $("#username").val(username);
hope solve problem.
Comments
Post a Comment