How to pass value instead of access token in SignalR HubConnectionBuilder

Viewed 144

As it is mention in the MS doc you can authorize your SignalR with access token like this:

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chathub", {
        accessTokenFactory: () => {
            // Get and return the access token.
            // This function can return a JavaScript Promise if asynchronous
            // logic is required to retrieve the access token.
        }
    })
    .build();

and you can use in your backend code (with IUserIdProvider) like this

public class EmailBasedUserIdProvider : IUserIdProvider
{
    public virtual string GetUserId(HubConnectionContext connection)
    {
        return connection.User?.FindFirst(ClaimTypes.Email)?.Value!;
    }
}

I don't have a proper token to pass to the accessTokenFactory, but I have UserId on my client and I want to use this userIs with GetUserId method, so I could distinguish who the chat belongs to by UserId instead of whole Token. Is it possible to pass another value (or just a one claim) to HubConnectionBuilder? Like for example:

let connection = new signalR.HubConnectionBuilder()
    .withUrl("/chathub", {
        accessTokenFactory: () => {
            // Pass your value or just a custom claim so it could be retrive on server side
        }
    })
    .build();
0 Answers
Related