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.
And on logout button click, write these codes.
So, if you still getting some kind of problem, please leave comment.
If you liked this article, please rate and comment. Thank You!


Deepesh Singh


18 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”);
    SqlCommand com = new SqlCommand(q, con);
    SqlDataReader read;
    read = com.ExecuteReader();
    u_name = read.GetValue(0).ToString();
    pass = read.GetValue(1).ToString();
    HttpCookie id1 = new HttpCookie(“u_name”);
    // HttpCookie pass1 = new HttpCookie(“pass”);
    id1.Value = u_name;
    //pass1.Value = pass;
    // Response.Cookies.Add(pass1);
    if (u_name != ” ” & pass != ” “)

    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();


    protected void Button1_Click(object sender, EventArgs e)
    Session[“username”] = null;
    Session[“password”] = null;


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

Please leave your valuable comment.

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

You are commenting using your 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