我的 UWP 应用程序有一个小问题。首先,UWP 应用具有以下功能:
- 企业认证
- 共享用户证书
- 专用网络
- 用户帐户信息
现在我想连接到 ASP.NET Core 2.1 Web API 中的 SignalR-Hub。集线器看起来像这样:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
namespace Test.Namespace
{
[Authorize]
public class SyncHub : Hub
{
public void SendUpdate()
{
Clients.All.SendAsync("Update");
}
}
}
这是我的 Startup.cs:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Test.Namespace
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSignalR();
services.AddSingleton<IUserIdProvider, NameUserIdProvider>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseSignalR(routes =>
{
routes.MapHub<SyncHub>("/syncHub");
});
app.UseMvc();
}
}
}
整个 API 在配置了 Windows 身份验证的 IIS 上运行。 Active Directory 在同一台机器上运行。
这就是我的 UWP 应用调用服务的方式:
HubConnection connection = new HubConnectionBuilder().WithUrl("http://Server:81/syncHub", options => {
options.UseDefaultCredentials = true;
}).Build();
await connection.StartAsync();
这个调用总是抛出 401。
我做错了什么?我在这个问题上工作了一个多星期,但我不明白为什么它不起作用。
感谢所有帮助我的人:)
编辑:所以我今天尝试了一些想法,发现这不是 SignalR 本身的问题。我用完全相同的调用创建了一个 ASP.NET Core 控制台应用程序,一切正常美好的。当我在 UWP 应用程序中对凭据进行硬编码时,它也有效。仅当我在 UWP 中使用“UseDefaultCredentials”时它才起作用。我完全没有头绪。我已经重新检查了这些功能,但这也无济于事。
最佳答案
app.UseHttpsRedirection();
似乎无法重定向客户端凭据。
尝试使用 https url 进行测试。
var hubConnectionBuilder = new HubConnectionBuilder();
var hubConnection = hubConnectionBuilder.WithUrl("https://localhost:44381/timeHub",options => {
options.UseDefaultCredentials = true;
}).Build();
await hubConnection.StartAsync();
关于c# - ASP.Net Core 401 中的 SignalR 未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53915092/