【问题标题】:SignalR in ASP.Net Core 401 UnauthorizedASP.Net Core 401 中的 SignalR 未经授权
【发布时间】:2018-12-24 15:13:30
【问题描述】:

我的 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”时它才不起作用。我完全一无所知。我已经重新检查了这些功能,但这也无济于事。

【问题讨论】:

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


    【解决方案1】:

    app.UseHttpsRedirection(); 似乎无法重定向客户端凭据。

    尝试使用 https url 进行测试。

    var hubConnectionBuilder = new HubConnectionBuilder();
    var hubConnection = hubConnectionBuilder.WithUrl("https://localhost:44381/timeHub",options => {
        options.UseDefaultCredentials = true;
    }).Build();
    await hubConnection.StartAsync();
    

    【讨论】:

      【解决方案2】:

      终于!

      答案真的很难找到,但现在它正在工作! 根据本站: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 站点,然后它就像一个魅力。

      感谢所有帮助我的人:)

      【讨论】:

        猜你喜欢
        • 2020-08-08
        • 2020-10-30
        • 1970-01-01
        • 1970-01-01
        • 2017-06-08
        • 2011-08-02
        • 2016-05-17
        • 2012-10-02
        • 2021-09-28
        相关资源
        最近更新 更多