有没有办法对连接到 Azure SignalR 的用户进行身份验证?

Is there a way to Authenticate users who connect to Azure SignalR?

我真的很难理解如何实现它。我有一个 Azure SignalR 服务 运行。我有一个 Azure 函数设置。我有一个网站 API.

Web API 向 Azure Function 触发 HTTP POST,然后使用 HTTPTrigger 向连接到 Azure SignalR 服务的所有用户发送(广播)。太棒了!

现在我遇到了一个问题,Web API 必须只向每个 HTTP POST 的特定用户发送消息。显然,这对我来说是用户在使用 Azure SignalR 连接(或“协商”)时需要以某种方式进行身份验证。

说实话,当我有一个自托管的 SignalR 应用程序时,我知道如何对用户进行身份验证。每当用户连接到 SignalR 集线器时,都要先使用不记名令牌身份验证来完成此操作。但是,那是自托管的 SignalR。我现在正在使用托管在 Azure 上的 Azure SignalR。

另外我需要说的是,Azure SignalR 的客户端侦听的 Negotatiate 和所有方法都是 Azure Functions。

[FunctionName("negotiate")]
    public static SignalRConnectionInfo Negotiate(
    [HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
    [SignalRConnectionInfo
        (HubName = "notifications")] //, UserId = "{headers.x-ms-client-principal-id}"
        SignalRConnectionInfo connectionInfo)
    {
        // connectionInfo contains an access key token with a name identifier claim set to the authenticated user
        return connectionInfo;
    }

    [FunctionName("PlacedOrderNotification")]
    public static async Task Placed(
    [QueueTrigger("new-order-notifications")] OrderPlacement orderPlacement,
    [SignalR(HubName = "notifications")] IAsyncCollector<SignalRMessage> signalRMessages,
    ILogger log)
        {
            log.LogInformation($"Sending notification for {orderPlacement.CustomerName}");

            await signalRMessages.AddAsync(
                new SignalRMessage
                {
                    Target = "productOrdered",
                    Arguments = new[] { orderPlacement }
                });
        }

我想获取我的不记名令牌身份验证并将其以某种方式放在 Azure SignalR 中。

现在是百万美元的问题……我到底该怎么做呢?我可以重新使用用于自托管 SignalR 服务的 Bearer Auth 代码并以某种方式将其集成到我的架构中吗?

正常的使用过程应该是下面描述的场景

The Web API fires a HTTP POST to the Azure Function and using a HTTPTrigger it then sends out (broadcasts) to all users who are connected to the Azure SignalR service. Great!

根据您的描述,Web API 发送一个 post 正文包含 json 数据,例如:

{
  "orderid": "0001",
  "status" : "created",
  "userid" : "Jason",
  ......
}

然后你的function app需要处理这个订单信息,找出需要推送的人才能收到这个消息。

Suggestion, you can integrate signlar with function app.

根据业务信息和需求,向指定用户推送订单信息。

1. SignalR - Checking if a user is still connected

2. Send message to specific user in SignalR