当有效用户登录系统并关闭浏览器而不注销时,它偶尔会(即不是紧接着但在第二天之后)阻止用户重新登录系统,抛出以下内容:
错误:403 - 禁止访问:访问被拒绝。您无权使用您提供的凭据查看此目录或页面。
这个question指的是同样的问题,但在他的解决方案中,他决定在创建FormsAuthenticationTicket时将 false 作为参数传递不使用持久性cookie,这不是理想的解决方案
这就是我创建cookie的方式:
private void createCookie(string username, int customerID, bool persist)
{
HttpCookie cookie = FormsAuthentication.GetAuthCookie(username, persist);
cookie.Expires = DateTime.Now.AddHours(12);
var ticket = FormsAuthentication.Decrypt(cookie.Value);
var userData = customerID.ToString();
var newTicket = new FormsAuthenticationTicket(ticket.Version, ticket.Name, ticket.IssueDate, ticket.Expiration, ticket.IsPersistent, userData);
cookie.Value = FormsAuthentication.Encrypt(newTicket);
Response.Cookies.Add(cookie);
}
关于如何解决这个问题的任何想法?
答案 0 :(得分:1)
当有效用户登录系统并关闭浏览器时没有 有时会退出(,即不是紧接着,而是在 第二天)阻止用户重新登录系统...
我可能很密集,但代码的工作方式与实现方式不同吗?
即,在createCookie()
中:您指定cookie.Expires = DateTime.Now.AddHours(12);
,这标志着Cookie在发布后12小时到期。
在Asp.net 1.0中,如果设置了FormsAuthenticationTicket.IsPersistent
,则故障单将自发布之日起有效期为50年。
然而在Asp.net 2.0中已不再是这种情况。如果FormsAuthenticationTicket.IsPersistent
设置为false,则故障单的有效持续时间与会话超时时间相同。如果FormsAuthenticationTicket.IsPersistent
设置为true,则有效持续时间将默认为Forms Authentication timeout属性。您将到期时间设置为发布时间加上12小时,因此我希望机票在12小时后停止工作。假设您使用的是Asp.net 2.0+,希望这可以解释您所看到的行为。我建议尝试将过期时间延长到更长的时间,看看问题是否消失。
答案 1 :(得分:0)
在auth cookie中包含您自己的userData没有固有的问题。 在我们的一个网站中,我们使用asp.net登录控件,并添加以下事件监听器并取得了很大的成功:
protected void Login1_LoggedIn(object sender, EventArgs e)
{
//... unimportant code left out
//Update the users ticket with custom userInfo object
string userData = userInfo.Id.ToString("N");
HttpCookie cookie = Response.Cookies.Get(FormsAuthentication.FormsCookieName);
FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(cookie.Value);
FormsAuthenticationTicket newTicket =
new FormsAuthenticationTicket(
oldTicket.Version,
oldTicket.Name,
oldTicket.IssueDate,
oldTicket.Expiration,
oldTicket.IsPersistent,
userData,
oldTicket.CookiePath);
cookie.Value = FormsAuthentication.Encrypt(newTicket);
}