代码之家  ›  专栏  ›  技术社区  ›  DAG

信号器集线器未从客户端WPV(.netcore 3.1)接收用户

  •  0
  • DAG  · 技术社区  · 4 年前

    我有一个成功连接到集线器的WPF客户机,但我无法将客户机的用户传递到集线器。

    我的 connection.User?.Identity?.Name IUserIdProvider 退货 .

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

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

    正如我上面提到的 connection.User?.Identity?.Name; 返回null。

    我不知道还能做些什么来将用户名从我的客户端(WPF)传递到我的Hub。

    Startup.cs

    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");
            });
        }
    

    任何帮助都将不胜感激。

    编辑:

    services.AddAuthentication(IISDefaults.AuthenticationScheme);

    编辑:

    来自Microsoft文档:

    0 回复  |  直到 4 年前
        1
  •  0
  •   mm8    4 年前

    你需要 configure 你的ASP.NET核心应用程序通过调用 AddAuthentication ConfigureServices 方法 Startup

    services.AddAuthentication(IISDefaults.AuthenticationScheme);
    

    您还应该编辑 launchSettings.json 根据 docs

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