How to removing all sessions and cookies after user logout
I have five pages in website
Page 1
Page 2
Page 3
Page 4
Page 5
I want all pages must be accessible by user only when if user is login properly. If user is logout or is not login and try to open any page like page 1 or page 5. He must be redirected to login page. All session and cookies must be removed Even if he try to open page like this way he must not be able to access this page without proper login.
here is my code but it is not working properly
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["AuthToken"] != null && Request.Cookies["AuthToken"] != null)
{
if (!Session["AuthToken"].ToString().Equals(Request.Cookies["AuthToken"].Value))
{
// redirect to the login page in real application
Label1.Text = "You are not logged in.";
}
else
{
Label1.Text = "Congratulations !, you are logged in.";
Label1.ForeColor = System.Drawing.Color.Green;
//btnLogout.Visible = true;
}
}
else
{
Label1.Text = "You are not logged in.";
Label1.ForeColor = System.Drawing.Color.Red;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
Session["UserName"] = null;
HttpContext.Current.Session.Clear();
HttpContext.Current.Session.Abandon();
HttpContext.Current.Response.Cookies.Add(new HttpCookie("ASP.NET_SessionId", ""));
//Session.Clear();
// Session.Abandon();
// Session.RemoveAll();
Response.Redirect("frmLogin.aspx");
}