SignalR Hub 未从客户端 WPF (.netcore 3.1) 接收用户
SignalR Hub not receiving user from client WPF (.netcore 3.1)
我有一个成功连接到集线器的 WPF 客户端,但我无法将客户端的用户传递到集线器。
我的 connection.User?.Identity?.Name
在我的 class 实施中 来自 IUserIdProvider
returns null.
对于我的 WPF 客户端,我使用它来连接集线器:
_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) 传递到我的集线器。
顺便说一句,我的 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);
但问题仍然存在,身份用户 (IUserIdProvider) 在从 WPF 客户端调用时返回 null。我在本地使用 IISExpress 运行 API。
编辑:
来自 Microsoft 文档:
Windows Authentication is only supported by the browser client when using Microsoft Internet Explorer or Microsoft Edge.
所以我想知道这是否有可能将桌面作为客户端。我认为它应该可以工作,所以我想知道我是否仍然遗漏了一点,或者这是否是与使用 (3.1.3)
的 SignalR I#m 版本相关的错误
您需要 configure 您的 ASP.NET 核心应用程序才能使用 Windows 身份验证,方法是在 [=14= 的 ConfigureServices
方法中调用 AddAuthentication
] class:
services.AddAuthentication(IISDefaults.AuthenticationScheme);
您还应该根据 docs:
编辑您的 launchSettings.json
文件
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:52171/",
"sslPort": 44308
}
}
我有一个成功连接到集线器的 WPF 客户端,但我无法将客户端的用户传递到集线器。
我的 connection.User?.Identity?.Name
在我的 class 实施中 来自 IUserIdProvider
returns null.
对于我的 WPF 客户端,我使用它来连接集线器:
_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) 传递到我的集线器。
顺便说一句,我的 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);
但问题仍然存在,身份用户 (IUserIdProvider) 在从 WPF 客户端调用时返回 null。我在本地使用 IISExpress 运行 API。
编辑:
来自 Microsoft 文档:
Windows Authentication is only supported by the browser client when using Microsoft Internet Explorer or Microsoft Edge.
所以我想知道这是否有可能将桌面作为客户端。我认为它应该可以工作,所以我想知道我是否仍然遗漏了一点,或者这是否是与使用 (3.1.3)
的 SignalR I#m 版本相关的错误您需要 configure 您的 ASP.NET 核心应用程序才能使用 Windows 身份验证,方法是在 [=14= 的 ConfigureServices
方法中调用 AddAuthentication
] class:
services.AddAuthentication(IISDefaults.AuthenticationScheme);
您还应该根据 docs:
编辑您的launchSettings.json
文件
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:52171/",
"sslPort": 44308
}
}