SignalR icon indicating copy to clipboard operation
SignalR copied to clipboard

Adding Query String/Headers in Javascript Client

Open ggiebel opened this issue 9 years ago • 19 comments

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?

ggiebel avatar Jul 24 '17 20:07 ggiebel

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.

moozzyk avatar Jul 25 '17 16:07 moozzyk

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.

Havret avatar Oct 12 '17 21:10 Havret

This issue is about JavaScript client and you point to the documentation on C# client. Which are you asking about?

moozzyk avatar Oct 12 '17 22:10 moozzyk

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.

Havret avatar Oct 12 '17 22:10 Havret

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

moozzyk avatar Oct 12 '17 22:10 moozzyk

Ok, everything is perfectly clear. Cheers!

Havret avatar Oct 12 '17 22:10 Havret

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.

Eric-Larson avatar Oct 17 '17 04:10 Eric-Larson

You need to use https://github.com/aspnet/SignalR/blob/595f783857598c56056305d627b969b856367454/src/Microsoft.AspNetCore.Sockets.Http/HttpConnectionContextExtensions.cs#L14

moozzyk avatar Oct 17 '17 05:10 moozzyk

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"];;
}

Eric-Larson avatar Oct 17 '17 05:10 Eric-Larson

this is an extension method so you can call it just like this:

var httpContext = context.Connection.GetHttpContext()

moozzyk avatar Oct 17 '17 05:10 moozzyk

Is there a way to send data from the client in the query string like in previous versions?

$.connection.hub.qs = { 'MyKey' : 'MyValue' };

bigswede74 avatar Oct 25 '17 15:10 bigswede74

yes - just use the full url with querystring when creating the connection (e.g. new signalR.HubConnection("/chat?mykey=myvalue");

moozzyk avatar Oct 25 '17 16:10 moozzyk

@moozzyk thanks that works great.

bigswede74 avatar Oct 25 '17 22:10 bigswede74

@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 avatar Nov 12 '17 17:11 craigdmckenna

@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.

moozzyk avatar Nov 12 '17 20:11 moozzyk

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.

craigdmckenna avatar Nov 13 '17 23:11 craigdmckenna

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)

moozzyk avatar Nov 14 '17 00:11 moozzyk

My bad, I didn't look closely enough to see that webSockets and eventSource use HttpClient and just assumed otherwise. Thanks moozzyk.

craigdmckenna avatar Nov 14 '17 00:11 craigdmckenna

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

analogrelay avatar May 24 '18 18:05 analogrelay