aspnetcore: SignalR can not get user identifier from ClaimsPrincipal

I use ASP.NET 6 RC1 Angular template.

I try to send message to specific user by user identifier and it doesn’t push message to client.

await storeHub.Clients.User(userId).SendMessage(message);

I realized UserIdentifier is null on user’s connection and i wanted to set via my custom provider. The weird thing is below provider return null as identifier.

    public class UserIdProvider : IUserIdProvider
    {
        public string GetUserId(HubConnectionContext connection)
        {
            return connection.User.FindFirstValue(ClaimTypes.NameIdentifier); //returns null
        }
    }

But the code on controller side is able to find and return identifier as User.FindFirstValue(ClaimTypes.NameIdentifier)

Not sure i missed something in here. I need help 😦

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Yes, if you want to use Users in a Signalr context: storeHub.Clients.User(userId) then you need to have an authenticated SignalR connection.

Now after reading article and providing access token and authorizing fixed the issue. @BrennanConroy

But do i really need authorization for hub in my use case really ?