Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 会话_在Global.asax.cs中结束,未启动_C#_Asp.net_Session_Forms Authentication - Fatal编程技术网

C# 会话_在Global.asax.cs中结束,未启动

C# 会话_在Global.asax.cs中结束,未启动,c#,asp.net,session,forms-authentication,C#,Asp.net,Session,Forms Authentication,我有一个Asp.net web应用程序,使用FormsAuthentication进行用户登录 我想防止同一用户同时多次登录。 为此,我已将FormsAuthentication超时设置为15分钟,将Session.timeout设置为15分钟 当用户在未注销的情况下关闭浏览器时,或者如果用户在15分钟内处于非活动状态,则不会触发global.asax.cs文件中的Session_End()事件。我想更新Session_End()事件中的数据库字段 登录代码: if (Membership.Va

我有一个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;
    }
public bool ResetUserLoginStatus(字符串username=”“)
{
string sql=“UPDATE Person SET IsLogged=0,其中Person=@Person”;
PersonObject person=新PersonObject();
对象id=person.ExecuteScalar(sql,新字典(){
{“Person”,(!string.IsNullOrEmpty(用户名)?用户名:User.Name)}
}“人”);
返回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,该用户将无法再次登录


请建议。

会话结束实际上没有那么有用或可靠。首先,它只在接收到HTTP请求并呈现响应时在管道处理结束时触发。这意味着它不会为仅仅关闭浏览器的用户触发。此外,除了某些类型的会话状态之外,事件永远不会触发——例如,它不会与状态服务器或基于SQL的会话状态一起工作。底线是您不能依赖它来维护一个明确的“已登录”标志


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

请发布您的代码关闭浏览器将永远不会触发它。它将在至少15分钟的超时后发生,可能会更长。是的。浏览器关闭后,我等了30分钟。但它仍然没有触发会话_End()