if (this.ChkRememberme != null && this.ChkRememberme.Checked == true)
{
int timeout = rememberMe ? 525600 : 30; // Timeout in minutes, 525600 = 365 days.
var ticket = new FormsAuthenticationTicket(TxtUserName.Text, TxtPassword.Text);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = System.DateTime.Now.AddMinutes(timeout);// Not my line
cookie.HttpOnly = true; // cookie not available in javascript.
Response.Cookies.Add(cookie);
}
转到您的web.config并找到身份验证元素。您可以设置cookie的到期时间(以分钟为单位)那里,像这样的:
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login"
name="myCookie" <!-- optional, if you want to rename it -->
timeout="2880" /> <!-- expires in 48 hours -->
</authentication>
</system.web>
来源:how to apply "Remember Me" in c#
希望这有助于
编码愉快.. !!
我不会把密码放在cookie中使用另一个标记(只需用户名就足够了) - 看看这里http://stackoverflow.com/questions/2452656/asp-net-mvc-rememberme –
你可以使用另一个SO回答,像这样: http://stackoverflow.com/questions/5619791/implementing-remember-me-feature-in-asp-net-mvc – idlerboris
你能帮助我确切的代码..? –