I do not work LogOut button in C #

1

I'm making a link to "Close Session"

<li><a id="Logout" href="#" onclick="btLogOut" runat="server">Cerrar Sesión</a></li>

and with the method

    public void btLogOut()
{
    System.Web.Security.FormsAuthentication.SignOut();
    Session.Abandon();
    Response.Redirect("Login.aspx");
}

But it's not working for me, please tell me what I'm doing wrong.

    
asked by Oscar Diaz 06.12.2016 в 15:34
source

4 answers

1

Change your label <a> by <asp:LinkButton> to make it look like this and you can redirect to the desired event:

<asp:LinkButton ID="lnkLogOut" runat="server" onclick="btLogOut">Cerrar Sesión</asp:LinkButton>

If it does not work, in the line that I put you delete btnLogOut and give Ctrl + Barra espaciadora and it will show you a list of available events to make the link with public void btLogOut()

    
answered by 06.12.2016 в 16:45
0

Another one you can use would be this:

HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.User = null;
System.Web.Security.FormsAuthentication.SignOut();
    
answered by 06.12.2016 в 16:40
0

I see you use

 System.Web.Security.FormsAuthentication.SignOut();

and also:

Session.Abandon();

but in the end you redirect your page:

  Response.Redirect("Login.aspx");

If you are going to close the session, it is enough with Session.Abandon() when executing the Click event on your button:

public void btLogOut(object sender, System.EventArgs e)
{ 
    Session.Abandon(); 
}

It is important that to achieve the execution of the method you must use a asp:Button :

<li><asp:Button id="Logout" href="#" OnClick="btLogOut" runat="server">Cerrar Sesión</asp:Button></li>

or a asp:LinkButton :

<li><asp:LinkButton id="Logout" href="#" OnClick="btLogOut" runat="server">Cerrar Sesión</asp:LinkButton></li>
    
answered by 06.12.2016 в 16:32
0

With your current code you are trying to call an event on the client side or Javascript code, to be able to call server-side code you should use a server-side control like asp: Linkbutton or asp: button as it is in other answers.

Or with the current code, if I'm not wrong you can use onserverclick="btLogOut"

    
answered by 06.12.2016 в 17:06