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
你能幫助我確切的代碼..? –