【问题标题】:Is there a way to Authenticate users who connect to Azure SignalR?有没有办法对连接到 Azure SignalR 的用户进行身份验证?
【发布时间】:2020-11-10 23:12:04
【问题描述】:

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

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

现在我有一个问题,对于每个 HTTP POST,Web API 必须只向特定用户发送消息。显然,当用户使用 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 代码并以某种方式将其集成到我的架构中吗?

【问题讨论】:

  • 您的问题解决了吗?有进展吗?

标签: c# azure azure-functions signalr azure-signalr


【解决方案1】:

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

Web API 向 Azure 函数触发 HTTP POST,然后使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    相关资源
    最近更新 更多