I have a website containing three aspx pages and a MasterPage
1) Login.aspx: containing a textbox and button when login I set a cookie with the text in the textbox
<asp:Label ID="lbl_EnterName" runat="server" Text="Enter name:"></asp:Label><br />
<asp:TextBox ID="txt_UserName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClick="btn_Submit_Click" />
and here's the code behind:
protected void btn_Submit_Click(object sender, EventArgs e)
{
HttpCookie UserCookies = new HttpCookie("Username");
UserCookies.Value = txt_UserName.Text;
UserCookies.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(UserCookies);
Response.Redirect("~/default.aspx");
}
====================
2) Default.aspx: containing a label shows the name in the cookie
<asp:Label ID="lbl_msg" runat="server"></asp:Label>
and here's the code behind:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie username = Request.Cookies.Get("Username");
try
{
lbl_msg.Text = "I'm logged in: " + username.Value;
}
catch
{
lbl_msg.Text = "Sorry. you're not logged in!";
}
}
====================
3) logout.aspx: just to Delete the cookie
here's the code behind
protected void Page_Load(object sender, EventArgs e)
{
Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);
}
====================
I want to run the Logout.aspx when closing the browser to re-login when open the website again (like facebook, gmail, ....)
I tried to use javascript code in the masterpage but it didn't work for me (events on the body tag -onunload- -onbeforeunload-) .. Thanks in advance