Base solution for your next web application
Open Closed

Signal R Authentication support #2296


User avatar
0
ianmark89 created

Hi,

Firstly, I created a simple signal R hub then I built a console client hub connection successfully connect to that hub without any authentication. E.g: static void Main(string[] args) { var querystringData = new Dictionary<string, string>(); querystringData.Add("foo", "bar"); var hubConnection = new HubConnection("http://localhost:62114/", querystringData); IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("MySampleHub"); stockTickerHubProxy.On<string>("messageRecieved", str => { Console.WriteLine("New Message: \n" + str); }); ServicePointManager.DefaultConnectionLimit = 100; hubConnection.Start(new LongPollingTransport()).GetAwaiter(); Console.ReadKey(); }

I need all clients connect to my hub must be authenticated then I set GlobalHost.HubPipeline.RequireAuthentication() in Web.Mvc project - Startup.cs file. The console client now can't receive message from server hub, by how I can have my console client authenticated :( ?

Any help would be appreciated. Thanks


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We did it using queryString in our angular2 template. Here you can see it <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero-angular/blob/3d834eb9d481097f09760eae8c98d514a61cc224/src/shared/helpers/SignalRHelper.ts#L8">https://github.com/aspnetzero/aspnet-ze ... lper.ts#L8</a>. We add token to query string, you can do the same thing in your console app.

  • User Avatar
    0
    ianmark89 created

    Yes, I passed the token via headers. Thanks for supporting.

  • User Avatar
    0
    ianmark89 created

    I tried this and it works :)

    var tokenString = "XXX";
    var hubConnection = new HubConnection("http://localhost:62114/");            
                hubConnection.Headers.Add("Authorization", "Bearer "+tokenString );
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)