Adding Query String/Headers in Javascript Client
I want to use a token for authentication when clients connect to a hub. I had been doing this with the old ASP.NET signalR and the jquery signalr client by adding a query string in the client and a server middleware that parsed the query string into a header. However, I cannot figure out how to set a query string or headers with the new signalr-client.js for .NET Core 2.0. Is this doable or has support been removed?
It has not been plumbed yet. The HttpClient supports headers but none of the transports are passing or taking headers. Also, LongPolling does not use the HttpClient. Note that the headers cannot be set when using websockets from javascript.
Any estimate when this will be implemented?
I would like pass a token before I establish my Hub Connection just like here:
https://docs.microsoft.com/en-us/aspnet/signalr/overview/security/hub-authorization#header
But HubConnection do not provide such interface.
This issue is about JavaScript client and you point to the documentation on C# client. Which are you asking about?
Sorry. I just wanted to make my point. :) I am asking about JS client, to be precise I would like to know how can I set my bearer token before establishing connection using HubConnection.
If you are using JavaScript you need to pass tokens using query string and this is already possible. Some of the underlying APIs SignalR is using (most notably WebSockets and EventSource) don't allow setting headers and there is not much SignalR can do about that. See also: https://github.com/aspnet/SignalR/issues/875#issuecomment-333390304, #888, #686
Ok, everything is perfectly clear. Cheers!
After attaching a query string on the client, I can't seem to find where I can retrieve the value on the server. I see ConnectionId within 'hub context', but no additional query strings.
You need to use https://github.com/aspnet/SignalR/blob/595f783857598c56056305d627b969b856367454/src/Microsoft.AspNetCore.Sockets.Http/HttpConnectionContextExtensions.cs#L14
Thanks @moozzyk! You posted the function signature from the socket namespace, here's the one for SignalR.
https://github.com/aspnet/SignalR/blob/64b3cf5339abf59e6dc327d450cc24fd5c21bf68/src/Microsoft.AspNetCore.SignalR/HttpConnectionContextExtensions.cs#L11
Here's what I used to retrieve the query string:
private string getQueryString(HubCallerContext context)
{
HttpContext httpContext = HttpConnectionContextExtensions.GetHttpContext(context.Connection);
return httpContext.Request.Query["token"];;
}
this is an extension method so you can call it just like this:
var httpContext = context.Connection.GetHttpContext()
Is there a way to send data from the client in the query string like in previous versions?
$.connection.hub.qs = { 'MyKey' : 'MyValue' };
yes - just use the full url with querystring when creating the connection (e.g. new signalR.HubConnection("/chat?mykey=myvalue");
@moozzyk thanks that works great.
@moozzyk, would you consider of the following API implementation for this issue?:
const conn = new signalR.HubConnection('/signalr', {
headers: {
'Accept-Language': 'en-US'
}
});
If so, I'd be willing to work on a PR.
@craigdmckenna - I had something similar (except I used Map<string, string> for headers) "working" on Friday and removed for now. The reason is that this is misleading - as explained above the browser does not allow setting headers for webSockets and eventSource, so users will be setting headers and expecting to see the values on the server side but in many cases they won't.
How about adding a constructor to HttpClient in order to initialize it with the headers option, which could then be passed to HttpConnection? HttpConnection has an existing httpClient option. This would be round-about but would isolate the headers option.
You can do it already (you can pass your own implementation of IHttpClient where you can set headers) but it does not solve the problem. The problem is:
- webSockets is a different API that does not allow setting the headers
- eventSource is a different API that does not allow setting the headers
- long polling is not using this client because it has more specific requirements (it was noted here: https://github.com/aspnet/SignalR/issues/3 but was done)
My bad, I didn't look closely enough to see that webSockets and eventSource use HttpClient and just assumed otherwise. Thanks moozzyk.
You shouldn't need to put it in the query string manually at all. Just specify an accessTokenFactory in the options object for .withUrl. Note, you'll need to manually configure JWT bearer auth to read from the query string. See the sample for more information: https://github.com/aspnet/SignalR/tree/dev/samples/JwtSample