且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在.NET Core中使用IUserIdProvider?

更新时间:2023-02-20 19:25:19

您需要实现IUserIdProvider

public class MyCustomProvider : IUserIdProvider
{
    public string GetUserId(HubConnectionContext connection)
    {
        ...
    }
}

则需要在Startup中注册以进行依赖注入:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSignalR();
    services.AddSingleton<IUserIdProvider, MyCustomProvider>();
}

那么SignalR将在HubEndPoint.OnConnectedAsync()这样的事件中使用UserIdProvider