2

我可以在 Global.asax 中处理表单身份验证超时吗?就像 global.asax 中的 Session_End 一样?请指教。

我在我的 webconfig 中的表单身份验证中设置超时:

<forms name="formName" loginUrl="Login.aspx" protection="All" path="/" timeout="30"/>

谢谢大家!:)

4

1 回答 1

2

不,您不能,因为超时是在身份验证 cookie 上编码的,并且存在于浏览器上(而不是在服务器端)。

您可以进行自定义,也可以将用户超时保留在数据库中 - 但它并不那么容易,或者您可以使用Application_AuthenticateRequeston global.asax 在请求之前检查用户是否不再经过身份验证。

如果用户未通过身份验证,如何删除会话数据的一个示例。在全球 asax 上。

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    // get the authCookie
    HttpCookie authCookie = Context.Request.Cookies[cookieName];
    // if is null then the use is not Authendicated
    if (null == authCookie && System.Web.HttpContext.Current.Session != null)
    {
        // now check if you have Session variables that you wish to remove.
        if(System.Web.HttpContext.Current.Session["flag"] == "1")
        {
            // remove your session data


        }   
    }
}

你也许也检查一下

if(HttpContext.Current.User == null || HttpContext.Current.User.Identity == null || !HttpContext.Current.User.Identity.IsAuthenticated)
{
        // now check if you have Session variables that you wish to remove.
        if(Session["flag"] == "1")
        {
            // remove your session data         

        }    
}
于 2013-05-14T05:28:36.620 回答