signalrsignalr-hubsignalr.client

Passing token through http Headers SignalR


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?


Solution

  • 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

    $.connection.hub.qs = { "token" : tokenValue };
    $.connection.hub.start().done(function() { /* ... */ });
    

    .NET Client

    var connection = new HubConnection("http://foo/",
                                       new Dictionary<string, string>
                                       {
                                           { "token", tokenValue }
                                       });
    

    Inside a Hub you can access the community name through the Context:

    Context.QueryString["token"]
    

    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

    var connection = new HubConnection("http://foo/");
    connection.Headers.Add("token", tokenValue);