1

I have created an Azure SignalR (Serverless) reosurce in azure portal. Then I have created an azure function HttpTrigger locally that references Microsoft.Azure.WebJobs.Extensions.SignalRService. In my azure function I have this code:

`public static class HttpTrigger
    {
        [FunctionName("Negotiate")]
        public static SignalRConnectionInfo Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [SignalRConnectionInfo(HubName = "notificationHub")] SignalRConnectionInfo connectionInfo,
            ILogger log)
        {
            log.LogInformation("Returning connection: " + connectionInfo.Url + "" + connectionInfo.AccessToken);

            return connectionInfo;
        }
        [FunctionName("Notify")]
        public static async Task<IActionResult> Notify([HttpTrigger(AuthorizationLevel.Function, "get", Route=null)] HttpRequest req,
                        [SignalR(HubName = "notificationHub")] IAsyncCollector<SignalRMessage> signalRMessage,
                            ILogger log)
                        {
                                log.LogInformation("Notify");
                                string msg = string.Format("Message from agent! {0} ", DateTime.Now);
                                await signalRMessage.AddAsync(
                                    new SignalRMessage
                                    {
                                        Target = "notifications",
                                        Arguments = new[] { msg }
                                    });

                                return new OkObjectResult("ok");
                        }

    }

` Also in my azure function, this is what my local.settings.json looks like:

`

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "AzureSignalRConnectionString": "myconnstringhere"
  },
  "Host": {
    "LocalHttpPort": 7071,
    "CORS": "http://localhost:53377",
    "CORSCredentials": true
  }
}

To also solve the CORS problem, I have added http://localhost:53377 domain of my client part project.

My client part is a separate asp.net web application project . So here I am connecting to this azure function like this:

`

<script>
    $(document).ready(function(){
        const connection = new signalR.HubConnectionBuilder()
        .withUrl("http://localhost:7071/api/")
        .configureLogging(signalR.LogLevel.Information)
        .build();

        connection.onclose(start);
        start(connection);
    });


    async function start(connection){
            try {
                    await connection.start();
                    console.log("SignalR connected.");
                    connection.on("notifications", (message) => {
                            $("#detailcontainer").html(message);
                            console.log(message)
                        });
                }
            catch(err) {
                            console.log(err);
                        }
    }
</script>

Now, I have published my azure function. But now it is not working anymore. It gets an error saying unauthorized when triggering /api/negotiate.

My azure function is a .net 6 project while the client app is a net framework 4.8. Is this because my client app is still in webforms?

I have added the connection string of my azure signalR to the application settings having a name format like this: Azure__SignalR__ConnectionString I also have configured CORS allowed origins for my azure function, I added my client localhost app.

1 Answers1

1

Closing this one because I found the answer. And it was really annoying that I have missed this one out.

I replaced AuthorizationLevel.Function to AuthorizationLevel.Anonymous. Because I am just passing the domain/api of my azure function and letting the signalR do its thing on their JS.