关闭浏览器时删除Cookie [英] Delete Cookie when closing the browser

查看:202
本文介绍了关闭浏览器时删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三个aspx页面和一个MasterPage的网站

1)Login.aspx:登录时包含一个文本框和按钮我在文本框中设置了一个带有文本的cookie

 <   asp:标签    ID   =  lbl_EnterName     runat   =  server   文字  = 输入名称: >  <   / asp:标签 >  <   br     /  >  
< asp:TextBox ID = txt_UserName < span class =code-attribute> runat = server > < / asp:TextBox >
< br / >
< asp:按钮 ID = btn_Submit runat = server 文字 = 提交 OnClick = btn_Submit_Click / >



,这是后面的代码:

  protected   void  btn_Submit_Click( object  sender,EventArgs e)
{
H ttpCookie UserCookies = new HttpCookie( 用户名);
UserCookies.Value = txt_UserName.Text;
UserCookies.Expires = DateTime.Now.AddDays( 1 );
Response.Cookies.Add(UserCookies);
Response.Redirect( 〜/ default.aspx);
}





====================

2)Default.aspx:包含标签显示cookie中的名称

 <   asp:标签    ID  < span class =code-keyword> =  lbl_msg    runat   =  server >  <   / asp:Label  >  



这里是背后的代码:

  protected   void  Page_Load( object  sender,EventArgs e)
{
HttpCookie用户名= Request.Cookies.Get( 用户名);

尝试
{
lbl_msg.Text = 我已登录: + username.Value;
}
catch
{
lbl_msg.Text = 抱歉。你还没有登录!;
}
}





================= ===

3)logout.aspx:只是为了删除cookie

这里是代码背后的代码

  protected   void  Page_Load( object  sender,EventArgs e)
{
Response.Cookies [ 用户名]。Expires = DateTime.Now.AddDays(-1);
}





================== ==



我想在关闭浏览器时再次登录时运行Logout.aspx再次打开网站(如facebook,gmail,....)

我试图在主页面中使用javascript代码,但它对我不起作用(身体标记上的事件-onunload- -onbeforeunload-)..提前谢谢

解决方案

如果您想在浏览器关闭时删除数据,为什么首先使用cookie? Cookie的重点是将数据保留到下次用户返回您的网站时 - 这与您尝试执行的操作完全相反!



据我所知,当用户关闭浏览器时无法测试,并且听起来像是你最好使用Session变量来存储用户的值 - 这些值会在浏览器或网页已关闭。


如果您将cookie设置为在创建它的过去日期到期,那么当浏览器关闭时它将删除cookie。



在您首次创建cookie的代码中,尝试更改此行...

 UserCookies.Expires = DateTime.Now。 AddDays( 1 ); 



......对此...

 UserCookies.Expires = DateTime.Now .AddDays(-1); 





...然后你就不需要这条线...

 Response.Cookies [ 用户名]。Expires = DateTime .Now.AddDays(-1); 


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

解决方案

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.


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...

UserCookies.Expires = DateTime.Now.AddDays(1);


... to this...

UserCookies.Expires = DateTime.Now.AddDays(-1);



... and then you will not need this line...

Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);


这篇关于关闭浏览器时删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆