ASP.NET:为什么FormsAuthenticationTicket在认证超时后为空?
我正在根据我之前的问题和答案实现身份验证超时检测机制。我已经实现了一个HTTP模块,该模块使用AuthenticateRequest事件来运行代码来捕获身份验证期是否已过期。执行此操作的代码如下:
public class AuthenticationModule : IHttpModule
{
#region IHttpModule Members
void IHttpModule.Dispose() { }
void IHttpModule.Init(HttpApplication application)
{
application.AuthenticateRequest += new EventHandler(this.context_AuthenticateRequest);
}
#endregion
/// <summary>
/// Inspect the auth request...
/// </summary>
/// <remarks>See "How To Implement IPrincipal" in MSDN</remarks>
private void context_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication a = (HttpApplication)sender;
HttpContext context = a.Context;
// Extract the forms authentication cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = context.Request.Cookies[cookieName]; // no longer a forms cookie in this array once timeout has expired
if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
DateTime expirationTime = authTicket.Expiration;
// check if previously authenticated session is now dead
if (authTicket != null && authTicket.Expired)
{
// send them a Response indicating that they've expired.
}
}
}
}
问题是,一旦身份验证期限到期(我将其设置为1分钟进行测试),就不再有表单cookie(请参阅代码中的注释)。这意味着身份验证cookie将为null,并且我不会通过我的代码中的空检查。但是对于FormsAuthenticationTicket,有一个方便的“Expired”属性,我觉得我应该检查一下这段时间是否过期。但是,如果cookie不再存在,我该怎么做呢?如果不再有表单cookie,是否合理假设认证期限已过期?
任何帮助将不胜感激。
没有找到相关结果
已邀请:
3 个回复
撵穆
更多信息 编辑: 1:我看到用户将为空。所以使用User.Identity是不可能的。 2:如何在BeginRequest事件中尝试原始问题中的代码而不是AuthenticateRequest。
秃拳割
邦绊门蔽惩