c# - ASP.Net Core 401 中的 SignalR 未经授权

标签 c# asp.net-core uwp signalr windows-authentication

我的 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/

相关文章:

c# - JsReport Recipe ChromePdf 页码

c# - 如何根据 Authorization header 值授权请求?

asp.net-core - 从 Asp.Net Core 中的 RouteCollection 中删除路由并添加具有相同路由名称的新路由 (nopCommerce-4.00)

c# - 为什么 EF Core 在末尾添加一个额外的 ORDER BY

c# - http ://ifconfig. me 如何从客户端获取所有这些信息?

c# - ASP.NET Web API 本地主机 :xxxx/api/product defaults to Home Page and not JSON data(orXML)

xaml - 如何在 ListViewItem 的 DataTemplate 中绑定(bind) IsSelected 属性

c# UWP - 将字节数组转换为 InMemoryRandomAccessStream/IRandomAccessStream

c# - 尝试将文件从 UWP 应用程序上传到 Flask restful web api,失败

c# - 如何将异步事件处理程序连接到 WinRT 中的非异步事件?