简体   繁体   English

使Cookie过期

[英]Making a cookie expire

I am trying to comply with new EU law for cookie legislation and need to remove all the cookies on my site before the user consents to it. 我正在尝试遵守有关Cookie立法的新欧盟法律,并且需要在用户同意之前删除我网站上的所有cookie。 Currently I've used the following successfully: 目前,我已经成功使用了以下内容:

function deleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
        var cookie = cookies[i];
        var eqPos = cookie.indexOf("=");
        var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
        document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
    }
}

However once this process runs there is still as a cookie called ASP.NET_SessionID which is created by ASP.NET. 但是,一旦运行此过程,ASP.NET仍会创建一个名为ASP.NET_SessionID的cookie。 To overcome this I set the following in the web.config file which basically stores the sessionID in the URL: 为了克服这个问题,我在web.config文件中设置了以下内容,该文件基本上将sessionID存储在URL中:

 <sessionState regenerateExpiredSessionId="true" cookieless="true" />

The problem I have is when I set cookieless="true" the javasscript code above doesn't remove any of my cookies anymore, its like its become redundant. 我遇到的问题是,当我设置cookieless =“ true”时,上面的javasscript代码不再删除我的任何cookie,就好像它变得多余一样。 When I set cookieless="false", the javascript works perfectly. 当我设置cookieless =“ false”时,JavaScript可以完美运行。 Are there any other settings I need to change to get both to work together? 我还需要更改其他设置以使它们一起工作吗?

Thanks 谢谢

If you don't need session, you can disable sessionstate (mode="Off") 如果不需要会话,则可以禁用会话状态(mode =“ Off”)

Also, I usually cleanup the cookies server-side, like this (never looked if it removed asp.net sessionid though) : 另外,我通常会像这样清理服务器端的cookie(但是从不检查它是否删除了asp.net sessionid):

protected void Page_Load(object sender, EventArgs e)
{
    Response.BufferOutput = true;
    Session.Abandon();
    HttpCookieCollection cookies = Request.Cookies;
    var cookiesList = new List<String>();
    foreach (String cookieKey in cookies)
        cookiesList.Add(cookieKey);

    foreach (var cookieKey in cookiesList)
    {
    var cookie = new HttpCookie(cookieKey);
    cookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(cookie);
    }
}

Hope this can help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM