Search code examples
asp.net-mvcsignalrsignalr-hubsignalr.client

SignalR Invoke() method "Unauthorized" error


I have added SignalR to our existing asp.net web application and everything is working fine. I've published this web application to Azure and now I'm getting Unauthorized error on the invoke method.
I've enabled web sockets and ARR affinity on Azure. I'm not using Authorize attribute. Do I need to add something else to my application to authenticate the web app or the user?

StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Set-Cookie: ARRAffinity=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;Path=/;HttpOnly;Domain=MyAppx.azurewebsites.net WWW-Authenticate: Bearer realm="MyAppx.azurewebsites.net" authorization_uri="https://login.windows.net/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/oauth2/authorize" resource_id="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" X-Powered-By: ASP.NET Content-Length: 58 Content-Type: text/html }


Solution

  • Because support for SignalR in asp.net is limited you must use some of the .net core libraries.Instead of using the invoke() method on the hub connection, you can take advantage of var context = GlobalHost.ConnectionManager.GetHubContext() which will eliminate a lot of pluming code. Using GlobalHost.ConnectionManager.GetHubContext solved the authorization issue as well.