我有一个WPF客户端,成功连接到Hub,但我不能把客户端的用户传递给Hub。
我的 connection.User?.Identity?.Name
在我的类中,从 IUserIdProvider
返回 无效.
对于我的WPF客户端,我使用这个来针对Hub进行连接。
_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");
});
}
任何帮助将是非常感激的。
EDIT:
我更新了代码。
services.AddAuthentication(IISDefaults.AuthenticationScheme);
但问题依然存在 当从WPF客户端调用身份用户(IUserIdProvider)时返回null。我在本地用IISExpress运行API。
EDIT:
从微软的文档中,
当使用Microsoft Internet Explorer或Microsoft Edge时,Windows身份验证仅由浏览器客户端支持。
所以我想知道是否可以用桌面作为客户端。我假设它应该工作,所以我想知道,如果我仍然错过了一个点,或者如果这是一个错误相关的SignalR版本,我#m使用(3.1.3)。
你需要 配置 您的ASP.NET Core应用程序通过调用 AddAuthentication
在 ConfigureServices
的方法 Startup
类。
services.AddAuthentication(IISDefaults.AuthenticationScheme);
你也应该编辑你的 launchSettings.json
根据 文件:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:52171/",
"sslPort": 44308
}
}