I can see that there is an option in HubConnection to pass parameters through url request from client. Is there any way to pass specific token through http headers from JS or .NET clients?
相关问题
- ASP.NET display progress bar during post back
- Server-side SignalR connection fails after signifi
- Can I use SignalR in my Winform c# app?
- signalR and large data transfer [closed]
- Why does an idle SignalR connection have 4-6 Mbps
There is no easy way to set HTTP headers for SignalR requests using the JS or .NET client, but you can add parameters to the query string that will be sent as part of each SignalR request:
JS Client
.NET Client
Inside a Hub you can access the community name through the Context:
You can add to the query string when making persistent connections as well.
EDIT: It is now possible to set headers on the .NET SignalR client as pointed out by some of the commenters.
Setting Headers on the .NET Client
You can add headers on either Connection or HubConnection using the .NET client (as @abnanda mentions):
On the server side you can retrieve the header value from IRequest in e.g. OnConnected:
However, I have not yet found a way to do this with the JS client (there is no headers or addHeaders on the $connection object).
I have posted a question on that at: http://forums.asp.net/t/1910020.aspx/1?Add+custom+HTTP+header+on+client+side+connect+call+SignalR+1+1+1+
EDIT: The headers are not exposed since the websocket client (in browser) does not support it (so it would not be possible to implement it across transports). See David Fowlers response in the post above.
With the js client, I don't think the header is supported but it must go via the querysting instead.
For the javascript client that is not the generated js proxy, you can add extra information to the querystring like this:
Messages such as ping, negotiate, abort, connect will now include this querystring value.
It's possible to send the header if you affect the ajax global setup. I have observed it making to the server but have not tested that it makes it in every case you might need.
I solved this by adding my information to the
querystring
, which is available on theIConnected.Connect()
method.On the .NET client you pass the
querystring into your HubConnection:
On the JS client you set the qs property before starting the connection:
You can then access this information on the server in the Connect() method: