【发布时间】:2012-06-08 08:50:06
【问题描述】:
表单身份验证票过期太快的另一个问题。 我需要使用滑动过期设置为真。我已经阅读了论坛并理解了精度损失的问题,即只有在过期时间的一半之后发出请求时才会更新票证。
问题: 在我的 webconfig 中,我有如下内容:
<authentication mode="Forms">
<forms timeout="20" name="SqlAuthCookie" protection="All" slidingExpiration="true" />
</authentication>
<sessionState timeout="20" />
<authorization>
只有在 20 分钟间隔内没有请求时,用户才必须注销并重定向到 login.aspx。问题是用户正在发出请求,但仍然被抛出到登录页面。这不应该发生。我想做的是为每个请求手动重置 SqlAuthCookie 。
下面是我的代码。它在 context.AcquireRequestState 上调用。
void context_AcquireRequestState(object sender, EventArgs e)
{
HttpContext ctx = HttpContext.Current;
ResetAuthCookie(ctx);
}
private void ResetAuthCookie(HttpContext ctx)
{
HttpCookie authCookie = ctx.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null)
return;
FormsAuthenticationTicket ticketOld = FormsAuthentication.Decrypt(authCookie.Value);
if (ticketOld == null)
return;
if (ticketOld.Expired)
return;
FormsAuthenticationTicket ticketNew = null;
if (FormsAuthentication.SlidingExpiration)
ticketNew = FormsAuthentication.RenewTicketIfOld(ticketOld);
if (ticketNew != ticketOld)
StoreNewCookie(ticketNew, authCookie, ctx);
}
private void StoreNewCookie(FormsAuthenticationTicket ticketNew, HttpCookie authCookie, HttpContext ctx)
{
string hash = FormsAuthentication.Encrypt(ticketNew);
if (ticketNew.IsPersistent)
authCookie.Expires = ticketNew.Expiration;
authCookie.Value = hash;
authCookie.HttpOnly = true;
ctx.Response.Cookies.Add(authCookie);
}
我的问题是:
- 在每个请求上重置 cookie 是错误的还是可接受的解决方案?
- 为什么还是不行?新票似乎永远不会更新。
- 是否还有其他可能的原因,因为用户的表单身份验证过早过期,我应该调查一下?
谢谢你, 问候,
【问题讨论】:
-
您使用的是什么版本的框架和 IIS?
-
项目的目标框架是4.0。 IIS 版本是 7,但我在 Visual Studio 2010 内置网络服务器上进行测试
-
我认为您应该在表单标签中提供域属性。无需在每个请求上写入更新 cookie
-
请详细说明为什么不应在每次请求时更新 cookie 的原因。我只是在查看有关域属性的文档,我看不出这如何有助于解决问题。谢谢
标签: asp.net forms-authentication httpcookie