SignalR cookies not sent from client

2019-04-12 21:13发布

问题:

I have a cookie which is sent from the client which is used as part of my MVC web service, however now that I have integrated a hub into this application the hub doesnt get sent the cookie, whereas the mvc app does.

Now after reading other similar questions (not that there are many) the cookies domain seems to be to blame, or the path is not set.

Currently my system has 2 web apps, the ui and service. In my dev environment it is like so:

Service http://localhost:23456/<some route>

UI http://localhost:34567/<some route>

So in the above example, the ui will send a query to the service, getting an authorisation cookie on the response, which is used elsewhere.

In this example the cookie domain from the service is localhost, as from what I have read and seen on other questions there is no need for a port, it will automatically just allow all ports.

Are HTTP cookies port specific?

SignalR connection request does not send cookies

So it would appear to me that the cookie above has correct domain, and the path is set to /, so it should work. However it doesn't send them in the request from javascript.

My request is a CORS request so I am not sure if there are any quirks around that but all normal jquery ajax calls make it to the server fine with the cookies, any ideas?

OH also my cookies are httponly as well, not sure if this makes a difference...

== Edit ==

Have tried to rule out some stuff, have turned off httponly and it still refuses to send the cookies to the server, I have also noticed a few outstanding cookie issues which mention adding the following code in somewhere to make ajax behave a certain way:

$.ajax({
                xhrFields: {withCredentials: true}
            })

Tried using that and still no luck, so I am out of ideas.

回答1:

I raised an issue as there is an underlying issue with < version 2 beta of SignalR relating to CORS and cookies.

https://github.com/SignalR/SignalR/issues/2318

However you can manually fix this issue by appending:

xhrFields: {withCredentials: true}

to all ajax requests within the jquery.signalr-*.js, this will then send cookies over CORS, although I do not know if this has any adverse effects on older browsers or IE.