Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.NET
<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:
C#
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.NET
<asp:Label ID="lbl_msg" runat="server"></asp:Label>

and here's the code behind:
C#
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
C#
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
Posted

Why are you using cookies in the first place if you want to remove the data when the browser closes? The whole point of a cookie is to persist the data until the next time the user comes back to your site - which is the exact opposite of what you're trying to do!

As far as I'm aware there is no way of testing when the user closes the browser, and it sounds to me like you'd be better off using Session variables to store your user's values - which are automatically destroyed when the browser or web page is closed.
 
Share this answer
 
v3
Comments
idenizeni 9-Oct-13 11:52am    
Not completely true as cookies can be used for a session only, you just need to give them a past expire date when they are created.
If you set the cookie to expire at a past date when it's created then when the browser closes it will remove the cookie.

In the code where you first create the cookie, try changing this line...
C#
UserCookies.Expires = DateTime.Now.AddDays(1);

... to this...
C#
UserCookies.Expires = DateTime.Now.AddDays(-1);


... and then you will not need this line...
C#
Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900