Shared secret authorization with Azure SignalR Service
While testing out Azure Functions development and configuration with Azure SignalR Service, I needed a very simple key-based (shared secret) authorization mechanism so that my console-based SignalR client could connect to my Azure SignalR Service-powered hub using a very simple mechanism.
The docs and the sample showcased the negotiate endpoint returning the connection info directly:
1
[FunctionName("negotiate")]
2
public SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequest req)
Copied!
But a stackoverflow answer pointed me to the solution: just the proper IActionResult using OkObjectResult with the connection info when the access key is properly validated:
1
[FunctionName("negotiate")]
2
public IActionResult Negotiate(
3
[HttpTrigger(AuthorizationLevel.Anonymous, "post")] HttpRequest req,
4
[SignalRConnectionInfo(HubName = "events")] SignalRConnectionInfo connectionInfo)
5
{
6
var expectedKey = Environment.GetEnvironmentVariable("AccessKey");
7
if (string.IsNullOrEmpty(expectedKey))
8
return new OkObjectResult(connectionInfo);
9
10
var accessKey = req.Query["accessKey"];
11
if (StringValues.IsNullOrEmpty(accessKey) ||
12
!StringValues.Equals(expectedKey, accessKey))
13
return new UnauthorizedResult();
14
15
return new OkObjectResult(connectionInfo);
16
}
Copied!
Last modified 1mo ago
Copy link