Tuesday, May 17, 2022

Logout button functionality in ASP.NET C#

 protected void Btn_logout_OnClick(object sender, EventArgs e)

    {

        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));

        Response.Cache.SetNoStore();


        Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1d);

        Response.Expires = -1500;

        Response.CacheControl = "no-cache";

        Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);


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

        if (Session["ID"] != null)

        {

            Session.Remove("ID");

        }

        if (Request.Cookies["trainingartnerCookies"] != null)

        {

            Request.Cookies["trainingartnerCookies"].Expires = DateTime.Now.AddDays(-1);

            new Cookies().RemoveCookies("trainingartnerCookies");

        }

        Session.Abandon();

        Session.RemoveAll();


        Response.Redirect("Login.aspx");


    }


or in simple


we can also use below code

Session.Abandon();

Response.Redirect("login.aspx");}

No comments:

Post a Comment