繁体   English   中英

Global.asax.cs中的Session_End不触发

[英]Session_End in Global.asax.cs not firing

我有一个Asp.net Web应用程序,其中使用FormsAuthentication进行用户登录。

我想防止同一时间多次登录同一用户。 为此,我将FormsAuthentication超时设置为15分钟,将Session.timeout设置为15分钟。

当用户关闭浏览器而不注销时,或者如果用户不活动15分钟,它将不会在global.asax.cs文件中触发Session_End()事件。 我想更新Session_End()事件中的数据库字段。

登录代码:

if (Membership.ValidateUser(username, password))
                {
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                        1,
                        username,
                        DateTime.Now,
                        DateTime.Now.AddMinutes(15),
                        false,
                        FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1"));

                    // Now encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    // Create a cookie and add the encrypted ticket to the cookie as data.
                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    context.Response.Cookies.Add(authCookie);

context.Response.Redirect("/HomePage", false);
}

的Global.asax.cs:

 protected void Session_Start(Object sender, EventArgs e)
    {
        Session["init"] = 0;
        Session.Timeout = 15;
     }
    protected void Session_End(Object sender, EventArgs e)
    {
        PersonObject person = new PersonObject();
       // calling the function to update entry in database
        person.ResetUserLoginStatus(HttpContext.Current.User.Identity.Name);
    }

更新数据库中条目的功能:

 public bool ResetUserLoginStatus( string username="")
    {
        string sql = "UPDATE Person SET IsLogged=0 WHERE Person =  @Person";
        PersonObject person = new PersonObject();
        object id = person.ExecuteScalar(sql, new Dictionary<string, object>() {
            { "Person", (!string.IsNullOrEmpty(username)?username:User.Name )}
        }, "Person");

        return true;
    }

Web.config文件:

<authentication mode="Forms">
  <forms loginUrl="/Security/Login.ashx/Home" name="SecurityCookie" timeout="15" slidingExpiration="true">
  </forms>
</authentication>

<sessionState timeout="15" mode="InProc"></sessionState>

问题在于,当浏览器关闭时,不会调用ResetUserLoginStatus()方法,并且我无法将我的值重置为0。由于该字段尚未重置为0,因此该用户将无法再次登录。

请提出建议。

Session_End实际上不是那么有用或可靠。 一方面,它仅在接收到HTTP请求并已呈现响应时才在管道处理结束时触发。 这意味着它对于仅关闭浏览器的用户不起作用。 此外,除了某些类型的会话状态外,该事件永远不会触发-例如,它不适用于State Server或基于SQL的会话状态。 底线是您不能依靠它来维护明确的“已登录”标志。

相反,我将存储“收到的最后一页请求”时间戳。 然后,您可以推断出“已登录”标志的值; 在过去15分钟内提交请求的任何用户仍将登录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM