我有一个成功连接到集线器的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>();
}
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文档: