Search code examples
cookiesarcgisarcgis-runtimearcgis-runtime-net

Trying to get a cookie value which I set on ARCGIS online but not getting any value back?


I am trying to set a cookie in ESRI Arcgis online using ESRI runtime SDK for .net v100.

        var cookie = new CookieHeaderValue("customCookie", cred.Token);             
        var response = Request.CreateResponse(HttpStatusCode.OK, new    {
                                                                            token = cred.Token,
                                                                            expires = cred.ExpirationDate
                                                                        });
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");            
        response.Headers.AddCookies(new CookieHeaderValue[] { cookie });
        return response;

Now when I try to retrieve that cookie later on in subsequent requests using below I get null.

CookieHeaderValue cookie = context.Request.Headers.GetCookies("customCookie").FirstOrDefault();

I am wondering if there is another way to get the cookie which I set back?


Solution

  • Are you using v100?

    If yes, you can try the following code:

            ArcGISHttpClientHandler.HttpRequestBegin += (sender, request) =>
            {
                var cookieContainer = ((System.Net.Http.HttpClientHandler)sender).CookieContainer;
                var cookies = cookieContainer.GetCookies(request.RequestUri);
                var customCookie = new Cookie("customCookie", "someValue") { Domain = request.RequestUri.Host };
                bool foundCookie = false;
                foreach (Cookie cookie in cookies)
                {
                    if (cookie.Name == customCookie.Name)
                    {
                        foundCookie = true;
                        break;
                    }
                }
                if (!foundCookie)
                    cookieContainer.Add(customCookie);
    
            };
    

    ArcGISHttpClientHandler has an event HttpRequestBegin which is invoked on every request. You can use CookieContainer.GetCookies and Add to retrieve/add cookies.