有没有办法验证连接到Azure SignalR的用户?

问题描述

我真的很难理解如何实现这一目标。我正在运行Azure SignalR服务。我有一个Azure函数设置。我有一个Web API。

Web API将HTTP POST触发到Azure函数,并使用HTTPTrigger发送出去(广播)给连接到Azure SignalR服务的所有用户。太好了!

现在我有一个问题,对于每个HTTP POST,Web API必须仅将消息发送给特定用户。显然,这将使用户在使用Azure SignalR连接(或“协商”)时需要进行某种方式的身份验证。

说实话,当我拥有一个自托管的SignalR应用程序时,我知道如何认证用户。每当用户连接到Hub for SignalR时,就使用承载令牌身份验证进行此操作。但是,那是自托管的SignalR。我现在正在使用Azure上托管的Azure SignalR。

我还要说,Azure SignalR的客户端所接受的协商和所有方法都是Azure函数

[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代码并将其以某种方式集成到我的体系结构中吗?

解决方法

正常使用过程应为以下所述的情况。

Web API将HTTP POST触发到Azure函数,并使用HTTPTrigger发送出去(广播)给连接到Azure SignalR服务的所有用户。太好了!

根据您的描述,Web API发送一个帖子,该正文包含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