简体   繁体   中英

Session not ending on click of “OnLoggingOut” action

I am trying to log out of a session on a website with the following code. However, seemingly nothing happens when I click on the logout button.

here is my code:

<asp:LoginStatus ID="LoginStatus1" OnLoggingOut="Logout_Click"  runat="server" /> 

and c#:

public void Logout_Click(object sender, EventArgs e)
    {

        Session.Abandon();
        FormsAuthentication.SignOut();
        FormsAuthentication.RedirectToLoginPage();


    }

This code is not ending the session, and it isn't even redirecting to the login page.

Web.config:

<authentication mode="Forms" >
      <forms loginUrl="url"   timeout="20" domain="domain">
      </forms>
    </authentication>

Thanks guys!

According to MSDN , your OnLoggingOut method should have 1 parameter of type LoginCancelEventArgs .

The code you have in the Logout_Click method should be fine, but it's not getting called because your method signature is incorrect.

(object sender, LoginCancelEventArgs e)

is the actual signature for the OnLoggingOut method. and you do not need the call to session.abandon.

Write a below code on your button click event

protected void Page_Load(object sender, EventArgs e)
{
    HttpContext.Current.Session.Abandon();
    HttpContext.Current.Response.Cookies.Clear();
    FormsAuthentication.SignOut();

    Response.Redirect("~/LoginPage.aspx");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM