14

我有以下设置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);

但是,当我尝试读取 cookie 时,值为 Null。cookie 存在。如果检查,我永远不会通过以下内容:

         if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))

帮助?

4

9 回答 9

47

回邮后检查是否完成?如果是这样,您应该改为从 Request 集合中读取 cookie。

cookie 通过将它们添加到 Response.Cookies 中持久保存到浏览器,并从 Request.Cookies 中读回。

添加到 Response 的 cookie 只有在页面在同一个请求上时才能读取。

于 2008-10-16T09:17:35.313 回答
19

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.

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.

于 2010-10-11T22:03:32.023 回答
1

您是否厌倦了“请求”集合而不是“响应”集合?

if (Request.Cookies["localization"] != null && !string.IsNullOrEmpty(Request.Cookies["localization"].Value))
于 2008-10-16T09:26:46.417 回答
1

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.

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.

于 2010-10-07T11:23:29.970 回答
1

I think I know the answer.

Just REMOVE the action attribute in your <form> tag.

make it look like this: <form id="form1" runat="server">

instead of this: <form id="form1" action="DisplayName.aspx" runat="server">

You should then use Response.Redirect("DisplayName.aspx"); in your code.

于 2011-09-27T17:16:17.860 回答
0

试试这个片段 -

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();
}
于 2008-10-16T11:25:03.997 回答
0

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.

于 2008-10-16T14:29:17.133 回答
0

Would add this as a comment to Chris Marisic's answer but I don't have that privelage :-(

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

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

于 2013-03-20T21:24:32.787 回答
-1

使用 Response.Cookies.Add(cookie); 而不是 Response.Cookies.Set(cookie);

于 2008-10-16T09:54:54.770 回答