Asp.net mvc 3 使用搜索机器人发送HTTP标头后,服务器无法修改cookie

Asp.net mvc 3 使用搜索机器人发送HTTP标头后,服务器无法修改cookie,asp.net-mvc-3,cookies,web-crawler,Asp.net Mvc 3,Cookies,Web Crawler,我不确定这里发生了什么,但有时在尝试设置cookie时,我会收到一条错误消息“服务器无法在发送HTTP头后修改cookie”。据我所知,这主要是某种搜索机器人。机器人是否禁用了cookie或其他功能?当我禁用Cookie时,我无法复制它。我下面的代码在控制器中运行。看起来对吗 var cookie = new HttpCookie(Config.ApiCookie) { HttpOnly =

我不确定这里发生了什么,但有时在尝试设置cookie时,我会收到一条错误消息“
服务器无法在发送HTTP头后修改cookie”。据我所知,这主要是某种搜索机器人。机器人是否禁用了cookie或其他功能?当我禁用Cookie时,我无法复制它。我下面的代码在控制器中运行。看起来对吗

                var cookie = new HttpCookie(Config.ApiCookie)
                {
                    HttpOnly = true,
                    Secure = false,
                    Value = authenticationResponse[SessionKey].ToString()
                };

                if (HttpContext.Current.Response.Cookies[Config.ApiCookie] != null)
                {
                    HttpContext.Current.Response.Cookies.Set(cookie);
                }
                else
                {
                    HttpContext.Current.Response.Cookies.Add(cookie);
                }
if (HttpContext.Current.Response.Cookies[Config.ApiCookie] != null)
            {
                HttpContext.Current.Response.Cookies[Config.ApiCookie].Value = cookie.Value;
            }
            else
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

问题出在.Set上,它有问题。我使用了下面的代码,解决了这个问题

if (HttpContext.Current.Response.Cookies[Config.ApiCookie] != null)
            {
                HttpContext.Current.Response.Cookies[Config.ApiCookie].Value = cookie.Value;
            }
            else
            {
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

在设置cookie之前,您是否可能刷新任何数据(即发送响应)?这就是您描述的错误消息所包含的内容。我不这么认为,这就是为什么我感到困惑的原因。您有任何异步的东西吗?我在这里看到的唯一可以使任何东西无效的是“authenticationResponse[SessionKey]”。。。尝试将其剥离,将值设置为“test”或类似的内容