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

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

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

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

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

说实话,当我拥有自托管 SignalR 应用程序时,我知道如何对用户进行身份验证。每当用户连接到 SignalR 集线器时,先通过不记名 token 身份验证完成此操作。然而,那是自托管的 SignalR。我现在使用 Azure 上托管的 Azure SignalR。

此外,我还需要说一下,Negotiate 以及 Azure SignalR 客户端监听的所有方法都是 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 }
                });
        }

我想采用不记名 token 身份验证并将其以某种方式放置在 Azure SignalR 中。

现在是百万美元的问题......我到底该怎么做呢?我可以重复使用用于自托管 SignalR 服务的承载身份验证代码并将其以某种方式集成到我的架构中吗?

最佳答案

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

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 发送一个正文包含 json 数据的帖子,例如:

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

然后您的功能应用需要处理此订单信息并找出需要推送的人来接收此消息。

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

关于c# - 有没有办法对连接到 Azure SignalR 的用户进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64778148/

相关文章:

c# - 使用 WebBrowser 获取弹出信息

Azure DocumentDb 一致性级别建议

azure - 如何使用事件网格将 AZ Blob 存储与 App webhook 连接?

azure - 在 Azure DevOps 管道中,如何使用从 addSpnToEnvironment 参数返回的服务原理详细信息登录到 AZ CLI?

Azure时间触发器功能不每小时触发

powershell - 在 PS 脚本中的 "Could not load file or assembly ' AzureAD"行出现错误 'Import-Module "System.Windows.Forms"

azure-functions - 从函数应用访问虚拟网络中的 Cosmos DB

c# - 如何在 C# 中获取 Hotmail 联系人列表?

c# - 如何获得*互联网* IP?

c# - DataGridViewCell 边框颜色