Closed fandango57 closed 4 years ago
By default the user identifier is the NameIdentifier
claim. You can access the value used by SignalR in the Hub via Context.UserIdentifier
. If you want to change that you can provide your own IUserIdProvider
implementation to DI. https://docs.microsoft.com/aspnet/core/signalr/authn-and-authz?view=aspnetcore-3.1#use-claims-to-customize-identity-handling
Thanks Brennan,
The await Clients.User(Context.UserIdentifier).SendAsync does work to send a message back to sender. But the receiver is not receiving the message using Clients.User(receiverEmail) and my code does map the NameUserIdProvider:IUserIdProvider shown below (which works on server side MVC). I replaced that with the ClaimType.Email?Value as in your reference and neither works. You can see the NameUserIdProvider registered in startup class above and I replaced it with EmailBasedUserIdProvider.
Still no go. Maybe I'm having a bad hair day..... wait a minute I'm bald.
I'll investigate further.
public class NameUserIdProvider : IUserIdProvider
{
public string GetUserId(HubConnectionContext connection)
{
return connection.User?.FindFirst(ClaimTypes.Name)?.Value;
}
}
//Replace above with
public class EmailBasedUserIdProvider : IUserIdProvider
{
public virtual string GetUserId(HubConnectionContext connection)
{
return connection.User?.FindFirst(ClaimTypes.Email)?.Value;
}
}
Well, I should have read the complete section you referred me to. I missed that you needed to add an Email claim to the user. After a quick check, it looks like its working, I'll review again tomorrow.
Thanks Brennan.
I've created with Visual Studio a BlazorWebAssembly App with Authentication, ASP.NET Core hosted, PWA, selections.
In the Startup I have cookie policy for SignalR and the client authenticates with SignalR Hub and Context information can be obtained. Messages can be sent via Clients.Client(connectionId) or Clients.All but not to Client.User. Since user Identiy is authenticated and identity known I could create database or dictionary mapping email to connectionIds and send that way. No errors are thrown and authorization to hub is successful. I'm wondering what I am missing or with Blazor do you send only by connectionId and not via Client.User?
Below are Startup.cs, ChatHubTest.cs, ChatTest.Razor, Program.cs.
Thanks
Patrick
ChatHubTest.cs
ChatTest.razor
Program.cs