Passing token through http Headers SignalR

前端 未结 6 1039
陌清茗
陌清茗 2020-12-04 16:45

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 o

6条回答
  •  攒了一身酷
    2020-12-04 17:15

    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:

    var connection = $.hubConnection("http://localhost:3306/signalr");
    connection.qs = "somekey=somevalue";
    var hubProxy = connection.createHubProxy('hubname');
    

    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.

      $.ajaxSetup({
          beforeSend: function (xhr)
          {
             xhr.setRequestHeader("Authorization","Token token=\"abcdef\"");        
          }
      });
    

提交回复
热议问题