0
votes

I used to use HTTP Headers to pass some authentication data from my SignalR Client (Android) to our SignalR.

After updating my project to use the lastest source from GitHub, this technique has stopped working.

After some research, I noted that this happens because the new default transport used is websocket, and websocket donĀ“t allow us to use Http Headers.

So,

  1. Is there any way to use HTTP Headers with SignalR and WebSockets transport?
  2. If no, how could I pass some parameters to my server? Is there any other option available than using QueryStrings?

Thanks!

1

1 Answers

2
votes

In general you should be able to set headers in the client and it should send them to the server when the websocket is being opened (the connect request). Not sure what client you use but this this is possible when for sure with C# client. However, as opposed to other transports, sending or receiving messages when using websockets does not require creating new HTTP requests and therefore if you set headers after the websocket is opened they won't be sent to the server until the next time the client has to send an HTTP request which is either when the client needs to reconnect or when the connection is stopped. Another option (if your client does not support headers for websockets) is to send parameters using query string. On the server side you can get the request using the HubCallerContext.Request property which allows you accessing the query string like this (you can also read cookies the same way):

Context.Request.QueryString

Again, query string will only be sent to the server if the client is making an HTTP request, which in case of websockets after the connection is established happens when the connection is reconnecting or is being stopped.

Finally, you already have a connection to the server so maybe you can just send your parameters using this connection which should work regardless of the transport you are using.