编辑 2
表单身份验证使用的 cookie 称为“.ASPXAUTH”,默认设置为 30 分钟后过期。
转到您的web.config
并找到authentication
元素。您可以在那里设置 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>
或者
如果配置失败,请尝试这篇文章:链接
您需要清除任何现有的身份验证票并创建您的自定义票。如果用户选择了该选项,则归结为您需要执行的这段代码remember me
:
if (rememberMe)
{
// Clear any other tickets that are already in the response
Response.Cookies.Clear();
// Set the new expiry date - to thirty days from now
DateTime expiryDate = DateTime.Now.AddDays(30);
// Create a new forms auth ticket
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, loginModel.UserName, DateTime.Now, expiryDate, true, String.Empty);
// Encrypt the ticket
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
// Create a new authentication cookie - and set its expiration date
HttpCookie authenticationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
authenticationCookie.Expires = ticket.Expiration;
// Add the cookie to the response.
Response.Cookies.Add(authenticationCookie);
}