Click here to Skip to main content
14,869,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
 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();
}
}
Posted

1 solution

Hi,

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

Hope this helps you a bit.

Regards,
RK
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900