C# Cookie 在 ASP.net 中失去价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/207878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cookie loses value in ASP.net
提问by Joda
I have the following code that sets a cookie:
我有以下设置cookie的代码:
string locale = ((DropDownList)this.LoginUser.FindControl("locale")).SelectedValue;
HttpCookie cookie = new HttpCookie("localization",locale);
cookie.Expires= DateTime.Now.AddYears(1);
Response.Cookies.Set(cookie);
However, when I try to read the cookie, the Value is Null. The cookie exists. I never get past the following if check:
但是,当我尝试读取 cookie 时,该值为 Null。cookie 存在。如果检查,我永远不会通过以下内容:
if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))
Help?
帮助?
采纳答案by Aleris
The check is done after a post back? If so you should read the cookie from the Request collection instead.
回邮后检查是否完成?如果是这样,您应该改为从 Request 集合中读取 cookie。
The cookies are persisted to the browser by adding them to Response.Cookies and are read back from Request.Cookies.
通过将 cookie 添加到 Response.Cookies 并从 Request.Cookies 中读回它们,cookie 会持久保存在浏览器中。
The cookies added to Response can be read only if the page is on the same request.
添加到 Response 的 cookie 只有在页面处于同一请求时才能读取。
回答by aunlead
Have you tired "Request" collection instead of "Response" collection?
您是否厌倦了“请求”集合而不是“响应”集合?
if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))
回答by Aleris
use Response.Cookies.Add(cookie); instead of Response.Cookies.Set(cookie);
使用 Response.Cookies.Add(cookie); 而不是 Response.Cookies.Set(cookie);
回答by aunlead
Try this snippet -
试试这个片段 -
string locale = ((DropDownList)this.LoginUser.FindControl("locale"))
.SelectedValue;
HttpCookie myCookie = new HttpCookie("localization");
Response.Cookies.Add(myCookie);
myCookie.Values.Add("locale", locale);
Response.Cookies["localization"].Expires = DateTime.Now.AddYears(1);
& to read it -
&阅读它-
if (Request.Cookies["localization"] != null)
{
HttpCookie cookie = Request.Cookies["localization"];
string locale = cookie.Values["locale"].ToString();
}
回答by Chris Westbrook
if you're compiling in debug mode, turn on tracing for the pages in question and make sure the cookie is in the request collection. Set trace in the @page directive in the aspx file.
如果您在调试模式下编译,请打开相关页面的跟踪并确保 cookie 位于请求集合中。在 aspx 文件的 @page 指令中设置跟踪。
回答by Fil
I had a similar problem, I couldn't read cookies on postback. The issue for me was that I checked the Secure property of the cookie to true. It is said that when the Secure property of the cookie is turned on, it causes the cookie to be transmitted only if the connection uses the Secure Sockets Layer. I am not sure, however, how I was able to see the cookie in the browser the first time, but not on postback, considering that I wasn't transmitting over SSL. Anyhow, turning the cookie.Secure to false, solved the problem, and had cookies read on postback.
我遇到了类似的问题,我无法在回发时读取 cookie。我的问题是我将 cookie 的 Secure 属性检查为 true。据说当cookie的Secure属性开启时,会导致cookie只有在连接使用安全套接字层时才会被传输。但是,考虑到我没有通过 SSL 进行传输,我不确定我是如何第一次在浏览器中看到 cookie 而不是在回发时看到的。无论如何,将 cookie.Secure 设置为 false,解决了问题,并在回发时读取了 cookie。
Sorry if this doesn't have to do anything with your issue, I wanted to share this, because I spent some time looking how to resolve this.
对不起,如果这与您的问题无关,我想分享这个,因为我花了一些时间寻找如何解决这个问题。
回答by Chris Marisic
The most likely answer is seen on this post
最有可能的答案见这篇文章
When you try to check existence of a cookie using Response object rather than Reqest, ASP.net automatically creates a cookie.
当您尝试使用 Response 对象而不是 Reqest 检查 cookie 是否存在时,ASP.net 会自动创建一个 cookie。
Edit:As a note, I ended up writing software that needed to check the existence of cookies that ASP.NET makes a nightmare due to their cookie API. I ended up writing a conversion process that takes cookies from the request and makes my state object. At the end of the request I then translate my state object back to cookies and stuff them in the response (if needed). This alleviated trying to figure out if cookies are in the response, to update them instead, avoiding creating of pointless cookies etc.
编辑:请注意,我最终编写了需要检查 cookie 是否存在的软件,ASP.NET 由于其 cookie API 而成为噩梦。我最终编写了一个转换过程,该过程从请求中获取 cookie 并生成我的状态对象。在请求结束时,我将我的状态对象转换回 cookie 并将它们填充到响应中(如果需要)。这减轻了试图找出 cookie 是否在响应中的尝试,而是更新它们,避免创建无意义的 cookie 等。
回答by Jeriboy Flaga
I think I know the answer.
我想我知道答案。
Just REMOVE the action attribute in your <form>
tag.
只需删除<form>
标签中的 action 属性即可。
make it look like this: <form id="form1" runat="server">
让它看起来像这样: <form id="form1" runat="server">
instead of this: <form id="form1" action="DisplayName.aspx" runat="server">
而不是这个: <form id="form1" action="DisplayName.aspx" runat="server">
You should then use Response.Redirect("DisplayName.aspx");
in your code.
然后你应该Response.Redirect("DisplayName.aspx");
在你的代码中使用。
回答by Simon Molloy
Would add this as a comment to Chris Marisic's answer but I don't have that privelage :-(
将此作为评论添加到 Chris Marisic 的回答中,但我没有那个特权 :-(
Further to what Chris said in his edit about removing cookies from the request, to be able to read the newly created cookies value in a postback I ended up doing
除了克里斯在编辑中所说的关于从请求中删除 cookie 之外,为了能够在回发中读取新创建的 cookie 值,我最终做了
Private Sub SetPageSize(ByVal pageSize As Integer)
' Set cookie value to pageSize
Dim pageSizeCookie As HttpCookie = New HttpCookie(pageSizeCookieName)
With pageSizeCookie
.Expires = Now.AddYears(100)
.Value = pageSize.ToString
End With
' Add to response to save it
Me.Response.Cookies.Add(pageSizeCookie)
' Add to request so available for postback
Me.Request.Cookies.Remove(pageSizeCookieName)
Me.Request.Cookies.Add(pageSizeCookie)
End Sub
The Request.Cookies.Remove and Request.Cookies.Add lines making it work on postbacks
Request.Cookies.Remove 和 Request.Cookies.Add 行使其适用于回发