Why SignalR cannot perform client-side authentication with WIndows Authentication

Aspire 81 Reputation points
2023-11-30T01:27:08.1333333+00:00

I have a SignalR server and client written in .NET 6.0, the server is a self hosted Windows Service.

Now I want to use Windows Authentication for authentication, but the problem is that when the client connects to the server within the same machine, the Context.UserIdentifier got in Server side is null.

Does this mean that authentication has failed?

The client side code

using SignalRClient.Common;
using Microsoft.AspNetCore.SignalR.Client;
using System.Net.Security;

namespace SignalRClient.Connections
{
    public class SignalRClient
    {
        private HubConnection? _hubConnection;
        public async Task HubConnection_StartAsync()
        {
            _hubConnection = new HubConnectionBuilder()
                .WithUrl("http://10.224.10.10:12345/simpleHub/", (options) =>
                {
                    options.HttpMessageHandlerFactory = (handler) =>
                    {
                        if (handler is HttpClientHandler clientHandler)
                        {
                            clientHandler.ServerCertificateCustomValidationCallback +=
                            (sender, certificate, chain, sslPolicyErrors) => { return true; };
                        }
                        return handler;
                    };
                    options.UseDefaultCredentials = true;
                })
                .WithAutomaticReconnect()
                .Build();
            _hubConnection.Closed += HubConnection_Closed;
            _hubConnection.Reconnecting += HubConnection_Reconnecting;
            _hubConnection.Reconnected += HubConnection_Reconnected;
            _hubConnection.On
Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,766 Reputation points Volunteer Moderator
    2023-11-30T02:04:24.9066667+00:00

    You appear to not have provided a custom user id provider required with Windows authentication. See

    https://learn.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-8.0

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.