票据加密,有时候浏览器记不住cookie。求解决。
下面是源代码:
private static HttpCookie GetAuthCookie(string userData, string userName, bool remember)
{
var ticket = new FormsAuthenticationTicket(
3,
userName,
DateTime.Now,
remember ? DateTime.Now.AddDays(14) : DateTime.Now.AddMinutes(20),
remember,
userData,
FormsAuthentication.FormsCookiePath);
string encTicket = FormsAuthentication.Encrypt(ticket);
if (string.IsNullOrEmpty(encTicket))
{
throw new HttpException("Unable_to_encrypt_cookie_ticket");
}
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
cookie.Path = "/";
cookie.Secure = FormsAuthentication.RequireSSL;
cookie.HttpOnly = true;
if (domain != string.Empty)
{
bool urlIsDomain = HttpContext.Current.Request.Url.Host.EndsWith(domain);
if (urlIsDomain)
{
cookie.Domain = domain;
}
}
if (ticket.IsPersistent)
{
cookie.Expires = ticket.Expiration;
}
return cookie;
}
当ticket.IsPersistent设成true时,浏览器有时候记不住cookie。很郁闷,大家帮忙看看。为啥?
webconfig中的配置:
<authentication mode="Forms">
<forms loginUrl="/Account/Account/LogOn" timeout="10800" slidingExpiration="true" path="/" name="CHN" domain="tita.com" protection="None" cookieless="UseCookies" />
</authentication> --------------------编程问答-------------------- cookies不设置超时时间 默认浏览器关闭就消失 --------------------编程问答-------------------- 是的,关键有让记住密码的需求。所以把ticket.IsPersistent设成true。
但有的浏览器时不时的 就来一个记不住密码,或登录不上,退不出去等问题。纠结呀。 --------------------编程问答-------------------- 看看服务器时间与本地时间是不是一致 --------------------编程问答-------------------- 是一致的,客户有Google Chrome 浏览器,Mozilla Firefox,ie都遇到这种问题。是不是代码写的有问题呀。又不是老出现,偶尔会来一个这种问题。郁闷了。 --------------------编程问答-------------------- //cookie存放路径
cookie.Path = FormsAuthentication.FormsCookiePath; --------------------编程问答-------------------- cookie被禁用了?
补充:.NET技术 , ASP.NET