将自定义信息存储在Azure SignalR服务中(无服务器)SignalRConnectionInfoAttribute

时间:2020-05-21 00:27:30

标签: serverless azure-signalr signalr-service

我正在使用SignalR Serverless。客户端进行协商时,它将传递从API获得的AccessToken,然后通过JS HubConnectionBuilder将其提供给Authorization标头。

在要进行协商的Azure函数中,我使用所述API验证令牌,并在验证之后将userId放入SignalRConnectionInfoAttribute中,类似于此简化示例:

   [FunctionName("MessagesNegotiateBinding")]
    public static IActionResult NegotiatBinding(
        [HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
        IBinder binder,
        ILogger log)
    {

            var token = req.Headers["Authorization"];

            var userId = "userIdExtractedFromToken"; // goes to in-house auth API here

            var connectionInfo = binder.Bind<SignalRConnectionInfo>(new 
                SignalRConnectionInfoAttribute{HubName = "messages", UserId = userId});

            return (ActionResult) new OkObjectResult(connectionInfo);

    }

稍后,当连接的事件在EventGrid上发布后被拾取时,我需要访问该原始令牌。那时我在connectionInfo绑定中有userId,但是我也需要那个原始令牌。

为了克服这个问题,我将userId的格式更改为“ userId | token”。这感觉不好,这使我无法仅使用userId将消息发送给该用户,因此我不得不将每个用户添加到他们是唯一成员的组中。

是否还有另一种方法可以将我需要的令牌存储在Negotiate中,以便可以从EventGrid的ClientConnectionConnected事件中使用该令牌?原因是建立连接后,我需要再次调用API并传递令牌。

我无法更改客户端,否则我会在成功连接后从它们的侧面调用API。

0 个答案:

没有答案