Search code examples
c#htmlasp.netmaster-pages

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!


Solution

  • 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");
        }