Hi all,
I am trying to set persistent cookie but not being able to.
I have 2 pages, login.aspx where user will login themselves and a default.aspx where user will be redirected once username and password is confirmed.
Login.aspx code is :-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpCookie objcookie = Request.Cookies["maincookie"];
if (objcookie != null)
{
Response.Redirect("Default.aspx");
}
else
{
Response.Redirect("login.aspx");
}
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (txtusername.Text == "Akiii" && txtpassword.Text == "123")
{
HttpCookie objcookie = new HttpCookie("maincookie");
objcookie["username"] = txtusername.Text;
objcookie["password"] = txtpassword.Text;
objcookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(objcookie);
Response.Redirect("Default.aspx");
}
else
{
Response.Write("Please try again !");
}
}
And the default page code is:-
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
HttpCookie objcookie = Request.Cookies["maincookie"];
if (objcookie != null)
{
usernameshow.Text = objcookie["username"];
passwordshow.Text = objcookie["password"];
}
else
{
Response.Redirect("login.aspx");
}
}
}
protected void btnlogout_Click(object sender, EventArgs e)
{
HttpCookie objcookie = Request.Cookies["maincookie"];
objcookie.Expires = DateTime.Now;
Response.Cookies.Add(objcookie);
Response.Redirect("login.aspx");
}
My problem is that, until and unless i click the logout button in the default page, the user should be able to access the website without doing login again...!
Please help me regarding this!
Thanks and Regards
Akiii