0

I am not able to read a cookie on the client that I set on the server.

I am using C# / MVC and am handling the cookie logic outside of the controller.

I pass in the Response, and can see it is set after I call this method from the controller, but once the response gets sent to the client, this cookie is not present.

In the controller, I am calling the SetCookie method like this:

CookieHelper.SetCookie(Response);

I have also tried to pass in System.Web.HttpContext.Current.Response to no avail.

The method I am calling is:

public class CookieHelper {

    public static void SetCookie(HttpResponseBase Response)
    {
        HttpCookie cookie = new HttpCookie("jayscookie");
        cookie.Value = "a much longer string than this";
        cookie.Expires = DateTime.Now.AddDays(7);
        Response.Cookies.Add(cookie);
    }

}

I have tried HttpResponseWrapper as well.
I have also tried System.Net.Cookie instead of HttpCookie

My IIS Setting for Session State is represented by this line in the Web.Config:

<sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" cookieless="false"/>

I have tried without cookieless="false"

The Cookie Settings section of this application in IIS are:

Mode: UseCookies Name: ASP.NET_SessionId Time-out: 20 (minutes)

I have visited the following and tried solutions:
Why is my cookie not set?
Cookies not getting set in c#
Why are the cookies not being set when doing a Redirect?
Cookie not setting in asp.net mvc
How to set cookie value?
HttpWebRequest/Reponse return cookies sent

As well as several microsoft documentation pages, such as:
https://msdn.microsoft.com/en-us/library/ms178194.aspx

Is there anything I am missing, or any basic concept I am obviously not taking into account? Setting cookies on the client works fine, just not setting them on the server.

Community
  • 1
  • 1
jay
  • 177
  • 1
  • 12
  • Cookies that are sent to the browser in the Response right? But when they are sent back to you, on the next request, they're found in the Request. – Jesse Moreland Jan 16 '17 at 18:45
  • Are you doing a redirect? This is a usual case when a cookie "disappears". – Tasos K. Jan 16 '17 at 18:51
  • @Tasos K., I am not doing a redirect. – jay Jan 16 '17 at 18:53
  • @JesseMoreland, They are not being sent back, and therefore are not present in the Request. (Which is how I found out all this was happening.) – jay Jan 16 '17 at 18:55
  • Is there a `StateServer` configured at `127.0.0.1:42424` and is the ASP.NET state `service` running? Have you tried `InProc` (default) first to debug if it's code level vs config? – EdSF Jan 16 '17 at 19:39
  • @EdSF , It is configured, and the service is running. I can save values to the Session variable, but just not cookies. – jay Jan 16 '17 at 19:42

2 Answers2

2

The cookie.Value string was a different value in my application. The value in the application was far too big, and exceeded the limit for cookie size. I will have to figure out a different approach, but that was the issue.

The size limit for cookies is around 4KB, for any who may find this information useful.

jay
  • 177
  • 1
  • 12
-1

Server side can t access client side directly and vice versa. Sorry for poor explain but i'm in train

Dans
  • 216
  • 1
  • 7
  • I am aware of that. I am trying to check client side via a browser. Thank you though – jay Jan 16 '17 at 19:19