我在 c# 中将身份验证 cookie 设置为永久性的,结束日期为从现在起一年,但设置后不久就会过期。代码如下...
DateTime endDate = new DateTime();
endDate = DateTime.Now.AddYears(1);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
username,
DateTime.Now,
endDate,
true,
userId.ToString(),
FormsAuthentication.FormsCookiePath);
string encryptedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authCookie = new HttpCookie(
FormsAuthentication.FormsCookieName,
encryptedTicket);
authCookie.Expires = endDate;
Response.Cookies.Add(authCookie);
有什么想法吗?
最佳答案
我想通了...当我检查用户是否通过身份验证时,我使用了以下代码...
if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
return true;
}
当我删除第一个检查(即 HttpContext.Current.User != null)时,它开始工作。虽然我真的不明白当 HttpContext.Current.User 为 null 时 HttpContext.Current.User.Identity.IsAuthenticated 怎么可能是真的。
无论哪种方式,它现在都可以使用,所以没问题。
关于c# .net - 身份验证过早过期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1928122/