单击退出时删除 cookie
我正在使用下面的代码创建 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);
I am creating the cookie using the code below, How to read the txtusername value in another page and how to delete the cookie when I click sign out(code for sign out). I am new to programming please help.
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您绝对不应该将密码存储为cookie。这是一个非常大的安全威胁。要删除 cookie,您实际上只需要修改它并使其过期即可。您无法真正删除它,即从用户磁盘中删除它。请查看此文档。
这是一个示例:
You should never store password as a cookie. That's a very big security threat. To delete a cookie, you really just need to modify and expire it. You can't really delete it, i.e. remove it from the user's disk. Check out this documentation.
Here is a sample:
您不能直接删除 cookie,您必须将其设置为在当前日期之前过期:
您可以阅读有关它的更多信息 这里。
此外,我真的鼓励您不要编写自己的安全性,而是阅读 asp.net会员资格。更安全、更易于使用。正如我所见,您的安全模型存在许多缺陷。将密码以纯文本形式存储在 cookie 中确实非常糟糕。
编辑:
由于您现在更改了代码,因此必须执行以下操作才能删除 cookie:
You cannot directly delete a cookie, you have to set it to expire before the current date:
You can read more about it here.
Furthermore I really encourage you to not write your own security but to read up on asp.net membership. More secure and easier to use. As I can see many flaws in your security model. Storing the password in plain text in a cookie is really really bad.
EDIT:
As you now changed your code, you have to do this to remove the cookie:
就我而言,这段代码有效:
In my case this code worked:
仅供参考,这对我使用
Chrome 69
并启用了从您上次停下的地方继续
功能来说不起作用。 Firefox 也有类似问题。禁用此功能对我有用。请参阅
FYI this did not work for me using
Chrome 69
with theContinue where you left off
feature enabled. Similar issue with Firefox. Disabling this feature worked for me.See