如何在用户关闭浏览器后注销

问题描述:

 DBConnection dbcon = new DBConnection();
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtUserName.Focus();
    }
    public void Trace_Session()
    {
        Session["UserNameSession"] = txtUserName.Text;
        Session["TimeLogin"] = System.DateTime.Now;

        string UserNameSession = Session["UserNameSession"].ToString();
        DateTime TimeSession =Convert.ToDateTime(Session["TimeLogin"].ToString());

        string str = dbcon.Trace_Session(UserNameSession.ToString(), TimeSession);
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string str = dbcon.Login_Verify(txtUserName.Text, txtPassword.Text);
        if (str == "valid")
        {
            Trace_Session();
            Response.Redirect("~/frmHome.aspx");
        }
        else if (str == "invalid")
        {
            lblStatus.Text = "UserName/Password is incorrect";
            txtUserName.Focus();
            txtUserName.Text = string.Empty;
            txtPassword.Text = string.Empty;
        }
    }

protected void btnLogout_Click(object sender, EventArgs e)
{
DateTime TimeoutSession=DateTime.Now.toString();
FormsAuthentication.SignOut();
}
}





最好设置会话时间清除,即使你想在关闭时设置它你可以参考下面的代码

Hi,

Its better to set time for session clear, even though you want to set it while closing you could refer the below code
function CloseSession( )
{
    //Trigger logout button click event to force to logout the application
    document.getElementById('btnLogout').click();
}
window.onbeforeunload = CloseSession;



希望这对你有所帮助。



问候,

RK


Hope this helps you a bit.

Regards,
RK