ASP.Net 核心 401 中的 SignalR 未经授权

SignalR in ASP.Net Core 401 Unauthorized

我的 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 的问题 itself.I 创建了一个 ASP.NET 核心控制台具有完全相同调用的应用程序,一切正常。当我在 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();

终于!

答案真的很难找到,但现在有效了! 根据这个网站:https://support.microsoft.com/en-us/help/303650/intranet-site-is-identified-as-an-internet-site-when-you-use-an-fqdn-o

该应用程序将该呼叫识别为对互联网的呼叫,因此不允许发送默认凭据。我必须使用 Internet Explorer 手动将此页面添加到本地 Intranet 站点,而且它的效果非常好。

感谢所有帮助过我的人:)