Question

I don't typically play with Cookies, but I wanted to look into this one verses the Session variables I typically use.

If I set a Cookie, then immediately try to read from it, I do not get the value back that I just set it to.

However, if I refresh the page or close the browser and open it back up, the Cookie appears to be set.

I'm debugging this in Chrome. Would that make any difference?

public const string COOKIE = "CompanyCookie1";
private const int TIMEOUT = 10;

private string Cookie1 {
  get {
    HttpCookie cookie = Request.Cookies[COOKIE];
    if (cookie != null) {
      TimeSpan span = (cookie.Expires - DateTime.Now);
      if (span.Minutes < TIMEOUT) {
        string value = cookie.Value;
        if (!String.IsNullOrEmpty(value)) {
          string[] split = value.Split('=');
          return split[split.Length - 1];
        }
        return cookie.Value;
      }
    }
    return null;
  }
  set {
    HttpCookie cookie = new HttpCookie(COOKIE);
    cookie[COOKIE] = value;
    int minutes = String.IsNullOrEmpty(value) ? -1 : TIMEOUT;
    cookie.Expires =  DateTime.Now.AddMinutes(minutes);
    Response.Cookies.Add(cookie);
  }
}

Below is how I use it:

public Employee ActiveEmployee {
  get {
    string num = Request.QueryString["num"];
    string empNum = String.IsNullOrEmpty(num) ? Cookie1 : num;
    return GetActiveEmployee(empNum);
  }
  set {
    Cookie1 = (value != null) ? value.Badge : null;
  }
}

This is how I am calling it, where Request.QueryString["num"] returns NULL so that Cookie1 is being read from:

ActiveEmployee = new Employee() { Badge = "000000" };
Console.WriteLine(ActiveEmployee.Badge); // ActiveEmployee is NULL

...but reading from Cookie1 is returning null also.

Is there a command like Commit() that I need to call so that a cookie value is immediately available?

Was it helpful?

Solution

To add to the other answers, you can get around the problem by caching the value in a private variable in case the cookie hasn't been updated yet:

private string _cookie1Value = null;
private string Cookie1 {
  get {
    if (_cookie1Value == null)
    {
      // insert current code
      _cookie1Value = cookie.Value;
    }
    return _cookie1Value;
  }
  set {
    // insert current code
    _cookie1Value = value;
  }
}

OTHER TIPS

Cookies are not like Session- there are two cookie collections, not one.

Request.Cookies != Response.Cookies. The former is the set of cookies that are sent from the browser when they request the page, the latter is what you send back with the content. This is exposing the nature of the cookies RFC, unlike Session, which is a purely Microsoft construct.

When you set a cookie in a response it does not get magically transported into the request cookies collection. It's there in the response, you're free to check for it there, but it won't appear in the request object until it actually is sent from the browser in the next request.

To simply put it; A cookie that is set in response, will only be available for the next htpp request (a next get or post action from the browser).

In detail: When a cookie value is set in HttpResponse, it will be persisted/stored only when after the response reaches client (meaning the browser will read the cookie value from Http Response header and save it). So, technically only for the henceforth requests it will be available. Example, when the user clicks on a link or a button that makes server call after this cycle from the browser.

Hope this gives you some idea, I suggest you read the What are cookies and ASP.NET wraps it before using it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top