从用户注销帐户后如何删除Cookie [英] How to delete cookies after signout user from his account

查看:75
本文介绍了从用户注销帐户后如何删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

当用户从其帐户注销时,我想从应用程序中删除Cookie,
有可能吗?
如果是,请建议我该怎么做?




感谢adv.

Hello guys,

I want to delete cookies from my application when user signout from his account,
is it possible?
if yes please suggest me the way how can i do this?




thanks in adv.

推荐答案

Cookie位于客户端计算机上.
如果用户从客户端结束会话,则可以尝试使用以下javascript.
如果用户只是关闭浏览器,或者浏览器意外关闭,我不确定该事件将触发.
您将无法通过Response的服务器端代码有效地执行此操作,因为这将需要回发,如果用户离开您的页面或关闭其浏览器,则不会发生回发.如果只想在显式注销时删除cookie,则可以使用Prasad的另一个答案(解决方案1),该答案显示了如何从服务器端进行操作.
Cookies are located on the client machine.
If the user ends their session from the client side you can try using the following javascript.
If the user simply closes their browser, or if the browser closes unexpectedly, I''m not sure the event will fire.
You won''t be able to do this effectively from the server side code from a Response because that would require a postback which does not happen if the user navigates away from your page or closes their browser. If you only want to remove the cookies on explicit sign-out, then you can use the other answer from Prasad(Solution 1) which shows how you can do it from the server side.
window.onbeforeunload = function() {
   var cookies = document.cookie.split(";");
   for (var i = 0; i < cookies.length; i++)
      eraseCookie(cookies[i].split("=")[0]);
};

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}


代码参考 [


Code Reference[^]


首先看看这个 MSDN [ ^ ]文章.

试试这个:
Firstly have a look on this MSDN[^] article.

Try this:
if (Request.Cookies["userId"] != null)
    {
        Response.Cookies["userId"].Expires = DateTime.Now.AddDays(-1);   
    }


但是使用
也很有意义


But it also makes sense to use

Session.Abandon();


此处找到它 [退出时删除Cookie [


Found it here[^]

Refer: Delete cookies on sign out[^]


这篇关于从用户注销帐户后如何删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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