SignalR的连接请求不发送cookieSignalR、cookie

2023-09-10 17:52:07 作者:后宫三千男妃

我建立使用的ASP.NET Web API和SignalR的Web应用程序。我有HTTP的pretty的好理解,但这个问题超出了我。

I am building a web application using ASP.NET Web API and SignalR. I have a pretty good understanding of HTTP but this problem is beyond me.

目前,我设置的所有的AJAX请求这个API的cookie。随后的AJAX请求到API发送cookie的时候没有问题。

Currently, I am setting a cookie on all AJAX requests to this API. Subsequent AJAX requests to the API send the cookie without issue.

不过,我想也可以在建立连接的SignalR请求中使用这个cookie,但是根据浏览器,cookie不会被发送这些请求。我有饼干设为仅Http。目前,一切都在本地运行;所有的请求转到本地主机:端口。在cookie中的域被设置为本地主机以及

However, I would like this cookie to also be used in the SignalR requests that establish a connection, but according to Chrome, the cookie is not being sent in these requests. I have the cookie set as HTTPOnly. Currently, everything is running locally; all requests go to localhost:port. The domain on the cookie is being set to localhost as well.

我SignalR的连接是这样的:

My SignalR connections look like this:

var connection = $.connection("/updates");
/* set handlers here */
connection.start(function () {
    console.log("connection started!");
});

看来,如果Chrome浏览器认为这是一个CORS请求,并扣留了饼干。该请求是在同一个域但是,所以这并没有太大的意义。

It seems as if Chrome thinks this is a CORS request and is withholding the cookies. The requests are on the same domain however, so this does not make much sense.

推荐答案

原来我不明白的cookies不够好。浏览器似乎有麻烦处理顶级域名如本地主机作为Cookie域。所以,我离开了域定义,因此该cookie将默认为请求的域。

Turns out I don't understand cookies well enough. Browsers seem to have trouble handling TLDs like localhost as the cookie domain. Therefore, I left the domain undefined, so that the cookie will default to the domain of the request.

不过,需要被设置为 / ,以确保该cookie在所有的请求发送的路径参数。

However, the path parameter needed to be set to / in order to make sure the cookie is sent in all requests.

在我做了这些改变,一切都如预期,和我的饼干是清晰可见的SignalR。

Once I made these changes, everything worked as expected, and my cookies were plainly visible in SignalR.

 
精彩推荐
图片推荐