试图获取我在ARCGIS上设置但未获得任何价值的cookie值?

时间:2017-03-21 17:35:04

标签: cookies arcgis arcgis-runtime arcgis-runtime-net

我正在尝试使用针对.net v100的ESRI运行时SDK在线设置ESRI Arcgis中的cookie。

        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;

现在,当我尝试使用下面的后续请求稍后检索该cookie时,我得到null。

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

我想知道是否还有另一种方法来获取我设置的cookie?

1 个答案:

答案 0 :(得分:1)

您使用的是v100吗?

如果是,您可以尝试以下代码:

        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有一个事件HttpRequestBegin,它会在每个请求中调用。您可以使用CookieContainer.GetCookies和Add来检索/添加cookie。