我意识到之前可能已经问过这个问题,但我找不到任何与我的情况完全匹配的问题。
我在ASP.Net网页(不网络表单)和WebMatrix中使用WebMail帮助程序创建了一个网站。用户需要登录该网站,并且有一个“记住我”框(理论上)将保持用户登录,直到他/她选择退出。如果用户关闭浏览器并在20-30分钟内重新打开,该网站会让用户保持登录状态。但是,在不访问网站20-30分钟后,用户将被注销。 (顺便说一句,即使使用WebMatrix模板“Starter Site”,这个问题似乎也存在。)
我尝试了多种解决方案,其中许多都发布在Stack Overflow上,但似乎没有任何效果。
答案 0 :(得分:12)
编辑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>
或强>
如果配置失败,请尝试以下文章:Link
您需要清除所有现有的授权凭证并创建自定义授权凭证。如果用户选择了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);
}
答案 1 :(得分:0)
您可以手动创建一个包含GUID的cookie(永不过期),该GUID映射到您的用户。当用户对您的用户登录页面进行GET时,您可以读取该cookie并检查guid并对用户进行身份验证。检查链接
http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/78c837bd(v=vs.100).aspx
http://www.codeproject.com/Articles/31914/Beginner-s-Guide-To-ASP-NET-Cookies