5

I am using library ng2-signalrin ionic 2. Issue is that i don't know how to set authorization header. I have search but didn't find any example.

My code for connecting to server hub.

   let options: IConnectionOptions = { qs:{userId:1}, url: "http://192.168.0.211:44337"};

        console.log("Stage 1");
            //Header for 'this.singalR.connect'
        this.signalR.connect(options)
            .then((connection) => {                      

                console.log("Client Id: " + connection.id);                     
             }, (err) => {
                console.log("SignalR Error: " + JSON.stringify(err));
            });

How to set below header ?

  var headers = new Headers({
            'Content-Type': "application/json",
            "Authorization": 'Bearer ' + accessToken  //accessToken contain bearer value.
        });

Library: ng2-signalr

Update 1

Here on link Query String Solution. A workaround is mention to pass authorization token in qs and handle accordingly. But i want to set in header so this solution does not suit me . One more reason as i have one another client(Simple angularjs signalr) which is working fine when i set header just like below.

 $.signalR.ajaxDefaults.headers = { Authorization: //here set header};  

Note: Before implementing Authorization same code working fine as i don't need to set authorization header.

Usman lqbal
  • 935
  • 1
  • 9
  • 31
  • did you try setting token in qs? – Suraj Rao May 31 '17 at 08:20
  • @suraj no not yet .oky i'm trying but that does not make a sense to set header values in query string `qs` . – Usman lqbal May 31 '17 at 08:46
  • https://stackoverflow.com/questions/21196266/signalr-authentication-with-javascript-client – Suraj Rao May 31 '17 at 08:47
  • @suraj given link is work around to pass token in query string and handle on server side accordingly. Right now it does not suit me because for that i need to change my server side code. if i change my server side then i have to change one more client(simple angularjs Signalr) which is working absolutely fine. – Usman lqbal May 31 '17 at 09:11

1 Answers1

1

It is currently (v2.0.4) not implemented in ng2-signalr.
As a workaround, you can try:

declare var $: any;
$.signalR.ajaxDefaults.headers = new Headers({
        'Content-Type': "application/json",
        "Authorization": 'Bearer ' + accessToken  //accessToken contain bearer value.
}); 
hannes neukermans
  • 12,017
  • 7
  • 37
  • 56