I am writing a dashboard like web application. Status changes of external systems should pushed down to the browser via SignalR. The external systems send their updates to Azure Service Bus topics. I have written an Azure Function which will be triggered by one of the topics. The function connects to the SignalR hub via the SignalR .Net Client library and forwards the message to the hub. The hub then sends the message to the browsers.
For now this works fine. The next step is to enable authentication for the SignalR hub. Other parts of the web app require authentication. The user logs in with his Azure AD credentials.
The question is how can the Azure function authenticate against SignalR? Saving some credentials in the app settings of the Azure Function App is a no go.
I have researched a workaround with the scale out technique of SignalR. We can configure a Service Bus Topic as a backplane. Each SignalR hub sends a copy of the message into the topic such that other instances of the hub get the message and push it down to their connected clients. The idea was that the Azure function pushes the status information into the backplane topic. But sadly SignalR uses an unknown encoding. So this workaround is not possible.
Details to @astaykov's answer
Add an app role to the SignalR's app registration.
"appRoles": [ { "allowedMemberTypes": [ "Application" ], "displayName": "Access SignalR Backend", "id": "239de039-e2c5-445c-8454-ccdc51888b94", "isEnabled": true, "description": "Allow the application to access SignalR Backend.", "value": "access" } ],
- Associate Azure Function App's app registration with the above app role. Make sure the Azure Function App's registration is of type Web app/API.
Create a key in the Azure Function App's app registration and use it when acquiring the token.
var ctx = new AuthenticationContext(tenant); var cred = new ClientCredential(functionAppRegistrationId, key); AuthenticationResult result = await ctx.AcquireTokenAsync(signalRRegistrationId, cred);
- Use the token in the query string. Search the internet for Bearer Authentication SignalR.