You can pass the jwt as a querystring parameter when you connect to the hub and then add the token as a header in your middleware. Heres a very basic example.
JS
let connection = new signalR.HubConnection('http://localhost/messagesHub', {jwtBearer: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ'});
connection.on('Send', data => {
console.log(data);
});
connection.start()
.then(() => connection.invoke('Send', 'test'));
Add to your Startup.cs Configure{} near the top, as high as possible
app.Use(async (context, next) =>
{
if (context.Request.Query.TryGetValue("token", out var token))
{
context.Request.Headers.Add("Authorization", $"Bearer {token}");
}
await next.Invoke();
});
Since the token is added in the header in your middleware, it should work as normal JWT auth with the same config as you would use in any other app.
Be aware you are passing a JWT in the querystring though so just be aware of the different security issues and make changes to help combat them (like https)