C# – Automatically sign out from Forms Authentication in ASP.NET when browser is closed

asp.netasp.net-membershipcforms-authenticationjquery

Is there a way to force ASP.NET to sign out from it's authentication when the browser is closed or the user types in a new address?

If the browser is left open then I need to keep the user authenticated, so a long timeout on the authentication ticket is preferable.

Best Answer

Not sure if this is still an issue but this resolved the issue for me. Just add the following to the Page_Load event of your Start Page:

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.UrlReferrer == null || string.IsNullOrEmpty(Request.UrlReferrer.AbsolutePath))
    {
        Session.Abandon();
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();
    }
}