Azure SIgnalR Service user credential validation before negotiation

472 Views Asked by At

We are trying to connect azure signalR service with our azure function app. We have a negotiate method in the function app that returns SignalRConnectionInfo object to client and before that, it makes a call to signalR service. We want to make a database call before making call to Azure signalR Service and returning SignalRConnectionInfo object. We are using Microsoft.Azure.WebJobs.Extensions.SignalRService 1.0.0 and Our negotiate function is like bellow

   [FunctionName("negotiate")]
    public static SignalRConnectionInfo GetSignalRInfo(
        [HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
        [SignalRConnectionInfo(HubName = "simplechat", UserId = "{headers.x-ms-signalr-userid}")] SignalRConnectionInfo connectionInfo)
    {
        return connectionInfo;
    }

seems library is taking userid from header and passing to signalR service. Is there any way to execute some code before letting the library making call to signalR service?

In Microsoft.Azure.WebJobs.Extensions.SignalRService 1.2.0 there are several ways to achieve this Like bellow.

 [FunctionName("negotiate")]
    public static SignalRConnectionInfo GetSignalRInfo(
        [HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req)
    {
        var userId = req.Query["userid"];
        var hubName = req.Query["hubname"];
        // Doing database check here 
        var connectionInfo = new SignalRConnectionInfo();
        var serviceManager = StaticServiceHubContextStore.Get().ServiceManager;
        connectionInfo.AccessToken = serviceManager
            .GenerateClientAccessToken(
                hubName,
                userId,
                new List<Claim> { new Claim("claimType", "claimValue") });
        connectionInfo.Url = serviceManager.GetClientEndpoint(hubName);
        return connectionInfo;
    }

Also by using class based hub

public class SimpleChat : ServerlessHub
{

    [FunctionName("negotiate")]
    public SignalRConnectionInfo Negotiate([HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req)
    {
        // Doing Db check here
        return Negotiate(req.Headers["x-ms-signalr-user-id"], GetClaims(req.Headers["Authorization"]));
    }

I want to achieve similar functionality using 1.0.0 of the library. Can someone help?

0

There are 0 best solutions below