简体   繁体   English

Cookie如何在ASP.NET中运行?

[英]How do Cookies Work in ASP.NET?

The website where I work is made up of several projects (written in several languages). 我工作的网站由几个项目组成(用几种语言编写)。 Right now we have to use some awkward code in query strings and session variables to keep a person logged in when they go from project to project. 现在,我们必须在查询字符串和会话变量中使用一些笨拙的代码,以便在人们从一个项目到另一个项目时保持登录状态。 Since cookies are domain specific we're trying to convert to them since they can be set in one project using one language yet be accessed by a different project (on the same domain) using a different language. 由于cookie是特定于域的,我们试图转换为它们,因为它们可以使用一种语言在一个项目中设置,但可以使用不同的语言由不同的项目(在同一域中)访问。

However I am having problems changing the value of a cookie and deleting them. 但是,我在更改cookie的值并删除它时遇到问题。 Or to be more specific, I'm having trouble having any changes I make to a cookie stick. 或者更具体地说,我无法对饼干进行任何更改。

For example in my logout code: 例如,在我的注销代码中:

if (Request.Cookies["thisuserlogin"] != null)
{
    HttpCookie myCookie = new HttpCookie("thisuserlogin");
    myCookie.Value = String.Empty;
    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);
    Response.Cookies.Set(myCookie);
    litTest.Text = myCookie.Expires.ToString() + "<br />" + Request.Cookies["thisuserlogin"].Expires.ToString();
}

I wind up with one line being yesterday and the next line being 1/1/0001 12:00:00 even though they SHOULD be the same cookie. 我结束了昨天的一行,下一行是1/1/0001 12:00:00,即使他们应该是相同的cookie。 So why is it that even though the cookie was set, it's value did not change? 那么为什么即使设置了cookie,它的价值也没有改变? Is there a way to force the user's computer to update a cookie's value, including deletion? 有没有办法强制用户的计算机更新cookie的值,包括删除?

Thank you very much. 非常感谢你。 PS Any URLs you can provide to give an easy-to-understand primer for cookies would be appreciated. PS您可以提供任何URL,以便为cookie提供易于理解的入门,我们将不胜感激。

http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx

if (Request.Cookies["thisuserlogin"] != null)
{
    HttpCookie byeCookie = new HttpCookie("thisuserlogin");
    byeCookie.Expires = DateTime.Now.AddDays(-1);
    Response.Cookies.Add(byeCookie);

    // Update Client
    Response.Redirect(Request.RawUrl);
}

You should use a tool like Fiddler on the client side to capture all of the data going back and forth. 您应该在客户端使用像Fiddler这样的工具来捕获来回的所有数据。 This will help you see that your cookie should be set with a date in the past (and missing from the next request too). 这将帮助您看到您的cookie应该设置为过去的日期(并且也会在下一个请求中丢失)。

As for your textbox output, you're listing the cookie you created expire time and the expire time of the request cookie, which doesn't have one. 至于您的文本框输出,您列出了您创建的过期时间的cookie和请求 cookie的过期时间,但没有。 If you were to look at the response cookie instead, you should see the date being set. 如果您要查看响应cookie,则应该看到正在设置的日期。 Also, the call to Response.Cookies.Set is unnecessary. 此外,不需要调用Response.Cookies.Set Response.Cookies.Add should be all you need. Response.Cookies.Add应该就是您所需要的。

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

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