IAmGroot
IAmGroot

Reputation: 13865

Access cookies immediately without causing duplication

Basically, I am trying to get a string value from cookie..

If I read it from just request.. I can get the value fine. Except for the first page load as the response hasn't gone to the client and back, so request doesn't have the value I just set.

However, strangely.. if I access Response and check if it exists (aka value just set).. I can get the value out. But it leads to some strange bug(?) where the cookie becomes duplicated in the request on next return.

On the second request back.. the value is read from the blank duplicated request cookie instead. (aka, cookie value does not work)

SAVE

var request = url.RequestContext.HttpContext.Request;
var response = url.RequestContext.HttpContext.Response;

//STORE VALUES TO COOKIE
HttpCookie hc = new HttpCookie("myCookie");
hc.Expires = DateTime.Now.AddDays(1);
hc.Value = "MYVALUE";

request.Cookies.Add(hc);
response.Cookies.Add(hc);

READ

public static string GetAdvancedCookieValue(this UrlHelper url, string cookieName)
{
    var request = url.RequestContext.HttpContext.Request;
    var response = url.RequestContext.HttpContext.Response;

    // Accessing Response allows me to get value just set.. but leads to duplicate cookies (with blank data -causing blank return later)
    if (response.Cookies[cookieName] != null && String.IsNullOrWhiteSpace(response.Cookies[cookieName].Value) == false)
        return response.Cookies[cookieName].Value;
    if (request.Cookies[cookieName] != null && String.IsNullOrWhiteSpace(request.Cookies[cookieName].Value) == false)
        return request.Cookies[cookieName].Value;

    return null;
 }

Upvotes: 1

Views: 1169

Answers (3)

Geraint Jones
Geraint Jones

Reputation: 1

With both cookie collections the way to check if a cookie exists is to check the name against the cookies collection AllKeys property.

Upvotes: -1

IAmGroot
IAmGroot

Reputation: 13865

For what ever reason, doing the null check response.Cookies[cookieName] != null seems to create the cookie... so I added a check that if the value is null, then remove it. So as to not post a blank cookie replacement to the client.

I can now access the most recent value, including those recently added to response, without wiping out cookie values.

    public static string GetAdvancedCookieValue(UrlHelper url, string cookieName)
    {
        var request = url.RequestContext.HttpContext.Request;
        var response = url.RequestContext.HttpContext.Response;

        if (response.Cookies[cookieName] != null)
        {
            if (response.Cookies[cookieName].Value == null)
                response.Cookies.Remove(cookieName);
            else
                return response.Cookies[cookieName].Value;
        }

        if (request.Cookies[cookieName] != null && String.IsNullOrWhiteSpace(request.Cookies[cookieName].Value) == false)
            return request.Cookies[cookieName].Value;

        return null;
    }

Upvotes: 1

rene
rene

Reputation: 42494

In your Save method you should check if the cookie with that name is already in the collection.

something like this:

var cookie = response.Cookies[hc.Name];
if (cookie == null) {
    response.Cookies.Add(hc);
} 
else
{
    cookie.Value = hc.Value;
    cookie.Expires = hc.Expires;
}

Remember that all cookies from the Request.Cookies collection are copied to the Response.Cookies collection. If you don't check for its existence before adding a cookie you'll end up with two cookies of the same name.

Upvotes: 1

Related Questions