1
Answer

Deactivate previous session

Photo of Anu

Anu

Sep 20
341
1

Hi,

I have set session timout to 10 min. On clicking logout I have used below codes to clear the session

 

At the time of each login, a new session id is generated. Even after regenerating new session and clear the previous session using the above code, the previously generated session id is active. Is there anything else to do for deactivating or killing previous sessions

 

Thanks,

Anu

Answers (1)

3
Photo of Jignesh Kumar
29 39.6k 2.9m Sep 21

Hello Anu,

If you are using form-based authentication then you need to do below to make sure your session will kill when you log out from syste,

protected void SignOut_Click(object sender, EventArgs e)
{
    
    Session.Clear();   
    Session.Abandon(); 

    // Sign out the user from Forms Authentication (if applicable)
    FormsAuthentication.SignOut();

    //Clear all cookies including authentication cookies
    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, "")
    {
        Expires = DateTime.Now.AddDays(-1), 
        HttpOnly = true
    };

    Response.Cookies.Add(authCookie);

    Response.Redirect("~/Login.aspx");
}