CoderT
CoderT

Reputation: 182

How to stop user from accessing the previous pages once he/she logs out

the code here is using ASP.NET with C#. The issue is that when user click logout button a user can return back to previous page.

Logoin code

  protected void Page_Load(object sender, EventArgs e)
    {
        Session["email"] = txtemail.Text;
    }

    protected void btlogin_Click(object sender, EventArgs e)
    {


    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "SELECT [email], [password] FROM [customer] WHERE [email]=@email AND [password]=@password";
    cmd.Parameters.Add("@email", SqlDbType.VarChar).Value = txtemail.Text;
    cmd.Parameters.Add("@password", SqlDbType.VarChar).Value = txtpassword.Text;

    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();

    if (reader.Read())
    {

        Response.Redirect("~/Booking.aspx");

        reader.Close();
        conn.Close();

    }
    else
    {

        lb.Text="Email or Password incorrect";

    }

}

}

logout code

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["email"] == null)
        {

            Response.Redirect("Default.aspx");

        }
    }

    protected void btlogout_Click(object sender, EventArgs e)
    {
        Session["email"] = null;

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

How to stop user from accessing the previous pages once he/she logs out

Upvotes: 2

Views: 196

Answers (1)

Dgan
Dgan

Reputation: 10285

There are several ways

Clear Your Session using Session.Abandon and use Response.Redirect("~/LoginPage.aspx");

Then you can use Following Methods to Clear Cache or clear history

Using Codebehind

// Code disables caching by browser.
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
Response.Cache.SetNoStore();

Using JavaScript

<SCRIPT LANGUAGE="javascript">
function ClearHistory()
{
     var backlen = history.length;
     history.go(-backlen);
     window.location.href = loggedOutPageUrl
}
</SCRIPT>

with asp.net

without update panel

Page.ClientScript.RegisterStartupScript(this.GetType(),
Guid.NewGuid().ToString(),"ClearHistory();",true);

with update panel

ScriptManager.RegisterStartupScript(this,this.GetType(),
Guid.NewGuid().ToString(),"ClearHistory();",true);

Upvotes: 1

Related Questions