本文介绍了在asp.net核心中使用signalR,如何更改userID约定以使用用户的电子邮件地址而不是默认的userId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用

    <PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />

在asp.net核心网络api中的

.默认情况下,我可以看到,如果我想向特定用户发送消息,我可以调用诸如

in an asp.net core web api. I can see by default that if I want to send a specific user a message I can call a method such as

_hub.Clients.User("c5MOTEmXsEZjoVf-y_sVQD_g-yBis9fvLMbyNJX3M31").SendAsync("MessageUpdated", "ee");

但是,我想知道,有什么方法可以更改此设置,以便我可以为userId传递不同的值吗?

However, I would like to know, is there any way to change this such that I can instead pass in a different value for the userId?

例如,改用用户的电子邮件地址吗?

For example, users email address instead?

这甚至有可能吗?

此外,如果我有一个用户的电子邮件地址,是否可以检测到userId是什么?也许第二个问题可以帮助我更好地解决我的特定问题.

Also, is it possible to detect what the userId is if I have a users email address? Perhaps this second question might help me solve my particular problem better.

推荐答案

这是解决方案

更新

public void ConfigureServices(IServiceCollection services)

包含

services.AddSingleton<IUserIdProvider, NameUserIdProvider>();

使用

public class NameUserIdProvider : IUserIdProvider
{
    public string GetUserId(HubConnectionContext connection)
    {
        return connection.User?.Identity?.Name;
    }
}

现在可以使用用户的电子邮件地址了

Now use can user email address in

_hub.Clients.User(userEmail).SendAsync

这篇关于在asp.net核心中使用signalR,如何更改userID约定以使用用户的电子邮件地址而不是默认的userId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:41