Amc*_*tty 11 asp.net forms-authentication httpcookie
表单身份验证票证的另一个问题是过早到期.我需要使用滑动Expiration设置为true.我已阅读论坛并了解精度损失的问题,如果请求仅在到期时间的一半之后进行,则只会更新故障单.
问题:在我的webconfig中,我有以下内容:
<authentication mode="Forms">
<forms timeout="20" name="SqlAuthCookie" protection="All" slidingExpiration="true" />
</authentication>
<sessionState timeout="20" />
<authorization>
Run Code Online (Sandbox Code Playgroud)
只有在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);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
谢谢,问候,
Mar*_*ark 14
表单身份验证cookie仅在到期时间的一半后自行续订.
来自微软:
如果在过期时间的一半之前访问网页,则不会重置故障单过期时间.例如,如果在下午5:04 00:00:00再次访问任何网页,则不会重置cookie和票证超时期限.
为了防止性能受损,并避免对打开了cookie警告的用户发出多个浏览器警告,当超过指定时间的一半时间后,cookie会更新.
这可能是你的问题.如果您的客户在9分钟内访问您的网站,并且在10分钟内不再访问您的网站,他们将会超时.即使您将会话超时设置为20分钟,也会发生这种情况.
不必像你正在做的那样手动更新你的机票.您只需要启用滑动过期.如果"特定时间的一半"规则对您不起作用,那么您将不得不查看其他解决方案.
| 归档时间: |
|
| 查看次数: |
8969 次 |
| 最近记录: |