简体   繁体   中英

How to get authenticated user id to use in Azure Function with Azure SignalR Service Output binding

I need to send SignalR message to the specific user id not all the clients who connected to the Azure SignalR service in Serverless mode . Below is the code where I want to set user id.

```
 [FunctionName("ProcessUpdate")]
 public static void Test([ServiceBusTrigger(topicName: "test", subscriptionName: "test", Connection = "AzureServiceBusConnectionString")] ServiceBusReceivedMessage  message,
         [SignalR(HubName = "Notification")] IAsyncCollector<SignalRMessage> signalrMessageCollector, ILogger logger)
        {           
            var signalrMessage = new SignalRMessage()
            {
               UserId = //assign authenticated user id
                Target = "receiveUpdate",
                Arguments = new[] { message.Body.ToString() }
            };
            signalrMessageCollector.AddAsync(signalrMessage);            
        }
```

I checked the documentation here

https://docs.microsoft.com/en-us/azure/azure-signalr/signalr-concept-serverless-development-config

It does not say how to access the user Id from Negotiate function in other azure functions that are event handlers for sending SignalR messages to clients.

I tried to use SignalRConnectionDetail sdk code in the output binding.Its not working mostly it seems that its part of the input binding.

App Service authentication sets HTTP headers named x-ms-client-principal-id and x-ms-client-principal-name that contain the authenticated user's client principal ID and name:

[FunctionName("negotiate")]
public static SignalRConnectionInfo Negotiate(
    [HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequest req, 
    [SignalRConnectionInfo
        (HubName = "chat", 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;
}

You can access them like this in the trigger above:

var id=req.Headers["x-ms-client-principal-name"].ToString();

SignalR Service input binding for Azure Functions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM