简体   繁体   中英

SignalR Hub not receiving user from client WPF (.netcore 3.1)

I have a WPF client which connects successfully to a Hub, but I cannot pass the user of the client to the Hub.

My connection.User?.Identity?.Name in my class implementing from IUserIdProvider returns null .

For my WPF client I use this to connect against the Hub:

_connection = new HubConnectionBuilder()
    .WithUrl(viewModel.Endpoint, opts =>
    {
        opts.Credentials = new NetworkCredential("user", "password", "domain");
        opts.UseDefaultCredentials = true;
    })
    .Build();

I have then the following provider registered as singleton:

    public class NameUserIdProvider : IUserIdProvider
    {
        public string GetUserId(HubConnectionContext connection)
        {
            return connection.User?.Identity?.Name;
        }
    }

As I mentioned above, the connection.User?.Identity?.Name; is returning null.

I don't know what else I can do to pass the user name from my client (WPF) to my Hub.

By the way, my Startup.cs looks like this:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();

        services.AddLogging();

        services.AddSingleton<IUserIdProvider, NameUserIdProvider>();

        services.AddSignalR(hubOptions =>
        {
            hubOptions.EnableDetailedErrors = true;
        });

        services.AddScoped<IBuildsService, BuildsService>();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthentication();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapHub<SyncCodeHub>("/signalr");
        });
    }

Any help would be much appreciated.

EDIT:

I update the code with:

services.AddAuthentication(IISDefaults.AuthenticationScheme);

But the problem continues, the identity user (IUserIdProvider) is returning null when called from the WPF client. I'm running the API locally with IISExpress.

EDIT:

From Microsoft docs:

Windows Authentication is only supported by the browser client when using Microsoft Internet Explorer or Microsoft Edge.

So I'm wondering if this is even possible with an Desktop as a client. I assume it should work, so I'm wondering if I'm still missing a point or if this is a bug related to the Version of SignalR I#m using (3.1.3)

You need to configure your ASP.NET Core app to use Windows authentication by calling AddAuthentication in the ConfigureServices method of the Startup class:

services.AddAuthentication(IISDefaults.AuthenticationScheme);

You should also edit your launchSettings.json file according to the docs :

"iisSettings": {
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": {
        "applicationUrl": "http://localhost:52171/",
        "sslPort": 44308
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM