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,是否合理假设认证期限已过期? 任何帮助将不胜感激。     
已邀请:
您可能想尝试这样的事情:
if (User != null)
        {
            FormsIdentity id = (FormsIdentity)User.Identity;
            FormsAuthenticationTicket ticket = id.Ticket;
            if (ticket.Expired)
            {
               //do something
            }
        }
更多信息 编辑: 1:我看到用户将为空。所以使用User.Identity是不可能的。 2:如何在BeginRequest事件中尝试原始问题中的代码而不是AuthenticateRequest。     
如果在FormsAuthenticationTicket上将If isPersistent设置为false,则不会设置持久性cookie。当故障单过期时,cookie不会随请求一起发送,因此您无法访问它。     
此行为由System.Web.Security.FormsAuthenticationModule控制。此模块检查故障单是否已过期,并在此情况下删除cookie。 另请注意,此模块检查slidingExpiration选项,如果需要,请更新故障单。 回到你的问题:   如果不再有表单cookie,是否合理假设认证期限已过期? 我认为回答是肯定的。     

要回复问题请先登录注册