Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to logout in asp.net using a html button?

I've created a masterpage where there is a button named sign out. How can I use that button in order to sign out or logout from the current login session.

Here is the button code:

<a href="#" class="btn btn-default btn-flat">Sign out</a>

Any help is greatly appreciated!

like image 725
Jilu Avatar asked Dec 02 '25 23:12

Jilu


1 Answers

use this logout code.

<a href="LogOut.aspx" class="btn btn-default btn-flat">Sign out</a>

LogOut.aspx

<form id="form1" runat="server">
    <asp:Label ID="Label1" Text="Loggin Out Please Wait" runat="server" />
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Timer ID="Timer1" runat="server" Interval="1000" OnTick="Timer1_Tick">
                </asp:Timer>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>

Logout.aspx.cs

protected void Timer1_Tick(object sender, EventArgs e)
    {
        Session.Clear();
        Session.Abandon();
        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();

        try
        {
            Session.Abandon();
            FormsAuthentication.SignOut();
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Buffer = true;
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1000;
            Response.CacheControl = "no-cache";
            //Response.Redirect("login.aspx", true);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        Response.Redirect("~/Login.aspx");
    }
like image 170
Genish Parvadia Avatar answered Dec 04 '25 13:12

Genish Parvadia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!