duta
duta

Reputation: 63

Authenticate C# Winforms app to Azure SignalR Service

I have an ASP.NET Core Web API wherein I host a SignalR hub.

I have created an Azure SignalR service, which the SignalR hub connects to at startup, using the connection string provided when I created the Azure SignalR service.

I have a C# winforms client app that connects to the SignalR hub at startup. The users of the Winforms app is authenticated using IWA.

I can start a local azure signalr service and point my C# winforms app to connect to that local service and everything connects fine.

Now, I want for the Azure SignalR Service to be able to authorize the incoming client connections. I do so by adding the Authorize] on the hub class level.

Now when the client connects it gets a 401 UnAuthorized error, which was expected.

I have created an App Registration for the API and the C# Winforms app in Azure, and I want to use these for authentication.

How do I do achieve this?

Upvotes: 1

Views: 128

Answers (1)

Sampath
Sampath

Reputation: 3639

The steps below are for authenticating with Azure SignalR Service using OAuth and deploying the application to Azure:

Prerequisites:

  • Create Accounts and Install Software:

    • Create a GitHub account.
    • Install Git.
    • Install .NET Core SDK.
    • Configure Azure Cloud Shell for the bash environment.

Create an OAuth App:

  • Create OAuth App on GitHub:

    • Go to GitHub > Settings > Developer settings > OAuth Apps.
    • Click "New OAuth App".
    • Fill in the details (name, homepage URL, description, callback URL).
    • Register the application.

Use the following DOC guide for authenticating Azure SignalR Service clients.

Homepage URL

https://myWebApp.azurewebsites.net

Authorization callback URL

https://myWebApp.azurewebsites.net/signin-github

  • Add Client ID and Secret:

    • Retrieve the Client ID and Client Secret.
    • Add them to the Secret Manager using the .NET CLI commands provided.Code is taken from git.

static async Task GetUserCompanyInfoAsync(OAuthCreatingTicketContext context)
{
    var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

    var response = await context.Backchannel.SendAsync(request,
        HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
    var user = await response.Content.ReadFromJsonAsync<GitHubUser>();
    if (user?.company != null)
    {
        context.Principal?.AddIdentity(new ClaimsIdentity(new[]
        {
            new Claim("Company", user.company)
        }));
    }
}

 public override Task OnConnectedAsync()
    {
        return Clients.All.SendAsync("broadcastMessage", "_SYSTEM_", $"{Context.User?.Identity?.Name} JOINED");
    }
    public Task BroadcastMessage(string message)
    {
        return Clients.All.SendAsync("broadcastMessage", Context.User?.Identity?.Name, message);
    }

    public Task Echo(string message)
    {
        var echoMessage = $"{message} (echo from server)";
        return Clients.Client(Context.ConnectionId).SendAsync("echo", Context.User?.Identity?.Name, echoMessage);
    }

Local:

enter image description here

Azure:

Add app settings for SignalR connection string and GitHub OAuth app secrets using Azure CLI.Update Homepage URL and Authorization callback URL of the GitHub OAuth app with the new hosted URLs.

enter image description here

Upvotes: 0

Related Questions