Back Button Problem in ASP.NET After Logout

I was facing a problem during one of my project. Actually, I was developing an E-Commerce site, in which I need to give login and logout button. I did login part easily and logout also. But problem arisen, when I pushed back button. Oh! God! Even after logout, it creates a session without login. How? I put all code behind ‘Logout’ button. But still I was facing the problem. We saw many codes, tried a lot of way to logout but still back button is not disabled.
One easy way to solve this problem is applying javascript. It would not allow a person to go back to that page. But, is it a feasible solution? NO! One would like to go back, even after logout to see something. This javascript would disable to come back. So, that is not a proper solution.
So, here I am writing the proper code, which we finally found out and happy news is that, “It really works!”. So just follow this code.
If you applying a MasterPage, then copy these codes to its page load, or copy to that page’s load, which comes after login page or comes before logout page.
pic11
And on logout button click, write these codes.
piv23>
So, if you still getting some kind of problem, please leave comment.
If you liked this article, please rate and comment. Thank You!

Signature

Deepesh Singh
logo

Advertisements

20 thoughts on “Back Button Problem in ASP.NET After Logout

  1. Thanks ,,, Thank u very much….
    it works well …
    but still 1 problem
    While Login perform some task to other pages and logout, till that ok now if i press back button Error is [Document Expire (Cache problem)]

    Only One Problem Left … !!!

    Please Help Me Out…

    • Dear friend, I am done my best to solve this problem. Please cross check once. You must be forgetting something. Please assure that you have pasted the code in the right place. And the variable I have for Session are same or changed according to your codes. Like Session[“user_id”]= null; You have to replace user_id with your parameter. Rest check the pages where you have pasted the codes. Please provide more information if problem not solved.

  2. Friend I log out successfully but when i click on back button there are show again welcome page. i don’t want to show this page after log out when i click on back button of browser.Please help me

  3. String q = “SELECT * FROM login WHERE username='” + TextBox1.Text + “‘ AND password='”+TextBox2.Text+”‘”;
    SqlConnection con = new SqlConnection(“Data Source=ankita-pc;Initial Catalog=portal;Integrated Security=True”);
    con.Open();
    SqlCommand com = new SqlCommand(q, con);
    SqlDataReader read;
    read = com.ExecuteReader();
    read.Read();
    u_name = read.GetValue(0).ToString();
    pass = read.GetValue(1).ToString();
    read.Close();
    con.Close();
    HttpCookie id1 = new HttpCookie(“u_name”);
    // HttpCookie pass1 = new HttpCookie(“pass”);
    id1.Value = u_name;
    //pass1.Value = pass;
    Response.Cookies.Add(id1);
    // Response.Cookies.Add(pass1);
    if (u_name != ” ” & pass != ” “)
    {
    Response.Redirect(“default.aspx”);
    }
    }

    this is my login page code

    protected void Page_Load(object sender, EventArgs e)
    {

    DateDisplay.Text = DateTime.Now.ToString(“dddd, MMMM dd”);
    L_id.Text = Request.Cookies[“u_name”].Value.ToString();
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
    try
    {
    Session.Abandon();
    FormsAuthentication.SignOut();
    Session[“username”] = null;
    Session[“password”] = null;
    Response.Redirect(“login.aspx”);

    }

    this is my logout page code…..but not working

  4. public partial class Adminpanel : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session[“id”] != null)
    {
    Response.Write(“HELLO”);
    }

    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    Session.Abandon();
    FormsAuthentication.SignOut();
    Session[“id”] = null;
    Session[“Pass”] = null;
    Response.Redirect(“~/login.aspx”);
    }
    }

    HERE IS MY CODE.
    and this is not working. Please check it what’s the problem in my code. Thank you

Please leave your valuable comment.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s