User customization with IUserIdProvider
SignalR’s IUserIdProvider
interface allows you to customize how user information is presented across your SignalR hubs without having to modify individual hub methods. By implementing a custom service that inherits from IUserIdProvider
, user information will be automatically enhanced when clients connect.
In this recipe, we will enhance our messaging application to include the user’s locale, taken from a claim on the JWT, alongside their username. We will do this without having to modify any of our hub methods. Our hub methods will continue to call Context.UserIdentifier
as normal, receiving both pieces of information automatically.
Getting ready
The starter project can be cloned here: https://github.com/PacktPublishing/ASP.NET-9-Web-API-Cookbook/tree/main/start/chapter06/IUserProviderLocale.
Please clone the starter project; the browser client and console client have been customized to test this recipe.