gpt4 book ai didi

c# - 单击退出时删除 cookie

转载 作者:太空狗 更新时间:2023-10-29 17:37:11 24 4
gpt4 key购买 nike

我正在使用下面的代码创建 cookie,如何读取另一个页面中的 txtusername 值以及如何在单击注销时删除 cookie(注销代码)。我是编程新手,请帮忙。

  string cookiestr;
HttpCookie ck;
tkt = new FormsAuthenticationTicket(1, txtUserName.Value, DateTime.Now,
DateTime.Now.AddMinutes(30), chkPersistCookie.Checked, "your custom data");
cookiestr = FormsAuthentication.Encrypt(tkt);

ck = new HttpCookie(FormsAuthentication.FormsCookieName, cookiestr);
if (chkPersistCookie.Checked)
ck.Expires = tkt.Expiration;
ck.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(ck);

最佳答案

您永远不应将密码存储为cookie。这是一个非常大的安全威胁。要删除 cookie,您实际上只需要修改它并使其过期即可。你不能真正删除它,即将它从用户的磁盘中删除。看看这个 documentation .

这是一个示例:

 HttpCookie aCookie;
string cookieName;
int limit = Request.Cookies.Count;
for (int i=0; i<limit; i++)
{
cookieName = Request.Cookies[i].Name;
aCookie = new HttpCookie(cookieName);
aCookie.Expires = DateTime.Now.AddDays(-1); // make it expire yesterday
Response.Cookies.Add(aCookie); // overwrite it
}

关于c# - 单击退出时删除 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7079565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com