【问题标题】:ASP.NET: Why is FormsAuthenticationTicket null after authentication timeout?ASP.NET:为什么 FormsAuthenticationTicket 在身份验证超时后为空?
【发布时间】:2011-05-28 07:12:03
【问题描述】:

我正在根据我之前的问题和答案here 实施身份验证超时检测机​​制。我已经实现了一个 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 将为空,并且我不会通过代码中的空检查。但是 FormsAuthenticationTicket 有一个方便的“过期”属性,我觉得我应该检查一下期限是否过期。但是,如果 cookie 不再存在,我怎么能走得那么远呢?如果不再有表单 cookie,假设身份验证期已过期是否合理?

对此的任何帮助将不胜感激。

【问题讨论】:

    标签: asp.net timeout forms-authentication


    【解决方案1】:

    你可能想尝试这样的事情:

    if (User != null)
            {
                FormsIdentity id = (FormsIdentity)User.Identity;
                FormsAuthenticationTicket ticket = id.Ticket;
                if (ticket.Expired)
                {
                   //do something
                }
            }
    

    More info

    编辑:

    1:我看到用户将为空。所以使用 User.Identity 是没有问题的。

    2:如何在 BeginRequest 事件中尝试原始问题中的代码,而不是 AuthenticateRequest。

    【讨论】:

    • 我实际上只是在您发布它之前尝试过,但是当身份验证期到期时,用户 is 为空。所以..再次..我无法检查票的 Expired 属性。
    【解决方案2】:

    如果在 FormsAuthenticationTicket 上将 isPersistent 设置为 false,则不会设置持久 cookie。当票证过期时,cookie 不会随请求一起发送,因此您无法访问它。

    【讨论】:

      【解决方案3】:

      此行为由 System.Web.Security.FormsAuthenticationModule 控制。该模块检查票证是否过期,在这种情况下删除 cookie。

      另请注意,此模块会检查slidingExpiration 选项,如果需要更新票证。

      回到你的问题:

      如果不再有表单 cookie,假设身份验证期已过期是否合理?

      我认为答案是肯定的。

      【讨论】:

        猜你喜欢
        • 2013-06-10
        • 1970-01-01
        • 2016-11-29
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 1970-01-01
        • 2017-04-16
        • 1970-01-01
        相关资源
        最近更新 更多