Client Authentication

Learn to authenticate clients in SignalR applications.

Authentications in SignalR

Only authenticated users can now access the hub. We now need to make sure that our clients can authenticate.

The JavaScript client

Because we are using cookie authentication, we don’t need to add anything extra to our JavaScript client. The back-end configuration ensures that the page is redirected to the login page if the user is not yet authenticated. Then the authentication cookie is being passed automatically in the request.

However, if we were to use our JavaScript client in a different context, we could get it to use the bearer token for authentication instead of a cookie. To do so, we would just need to add the following option to the withUrl call on HubConnectionBuilder:

Press + to interact
accessTokenFactory:()=>myToken

Here, myToken represents a token that we obtain from SSO provider. Obtaining the token outside the browser is beyond the scope of this article, as it’s not a subject that is specific to SignalR. However, information on it is available in the official OpenID Connect documentation.

The JavaScript client also has withCredentials option, which, if set to true, will apply direct credentials to the request that it can extract from a specific cookie. This is especially applicable for Azure App Service, which uses cookies for sticky sessions. This type of service will not work correctly without this option enabled.

And these are all the authentication parameters we can use in the JavaScript client.

The .NET client

...