首页 > 解决方案 > SignalR .net 核心在 Windows 内部网中获取 401 协商请求

问题描述

我正在尝试让 signalR 与 .net core 2.2 应用程序一起使用。我需要从有角度的前端连接到它。我有另一个应用程序,它的配置方式似乎与已经让 signalR 工作的方式相同。当我从角度连接到其他应用程序时,它可以工作,但是如果我尝试连接到原始应用程序,我会收到 401 错误。据我所知,涉及 signalR 的代码看起来相同。

我们使用windows内网认证。我无权访问最初设置它的任何开发人员,但它们都在我们的 Windows 内部网站点中,我们通过一个警报弹出窗口登录,我将我的内部网信息放入其中。

这是startup.cs中的相关代码配置服务

            services.AddCors();

        services.AddSignalR();

这是在 sertup.cs 中配置

var origins = Configuration.GetValue<string>("apiAllowedOrigins").Split(',');
        app.UseCors(builder =>
            builder.WithOrigins(origins)
                    .AllowAnyMethod()
                    .AllowCredentials()
                   .AllowAnyHeader()
            );

        app.UseAuthentication();

        app.UseSignalR(routes =>
        {
            routes.MapHub<IbjaHub>("/ibjahub");
        });

这里是 ibjahub

    using Microsoft.AspNetCore.SignalR;

namespace CapitalJobs.SignalR
{
    public class IbjaHub : Hub
    {
    }
}

这是连接到集线器的角度代码

 this._ibjaService
  .getSignalRHub()
  .pipe(
    tap(hd => {
      const hubConnection = new signalR.HubConnectionBuilder()
        .withUrl(hd.hubURL)
        .build();
      hubConnection
        .start()
        .then(() => {
          console.log('SignalR Connection started');
        })
        .catch(err => {
          this._toaster.pop({
            type: 'error',
            title: 'Connection Lost',
            body: `Connection to server lost, and attempts to reconnect failed. Please refresh the page.`,
            timeout: 0,
            bodyOutputType: BodyOutputType.TrustedHtml
          });
          console.error(err);
        });

      hubConnection.on(
        'todo: replace this with what its going to say',
        data => {
          if (data.username !== this._config.appMenu.userName) {
            return;
          }
          //todo parse the data about e1
        }
      );

      hubConnection.onclose((error?: Error) => {
        if (error) {
          console.error(error.message);
          window.setTimeout(() => {
            hubConnection
              .start()
              .then(() => {})
              .catch(err => {
                this._toaster.pop({
                  type: 'error',
                  title: 'Connection Lost',
                  body: `Connection to server lost, and attempts to reconnect failed. Please refresh the page.`,
                  timeout: 0,
                  bodyOutputType: BodyOutputType.TrustedHtml
                });
                console.error(err);
              })
              .then(() => {
                this._toaster.pop({
                  type: 'warning',
                  title: 'Connection Lost',
                  body: `Connection to server was lost, but was successfully reconnected. Some messages may have been lost. Please refresh the page if issues continue.`,
                  timeout: 0,
                  showCloseButton: true,
                  bodyOutputType: BodyOutputType.TrustedHtml
                });
              });
          }, 2000);
        }
      });
    })
  )
  .subscribe();

}

我使用相同的代码连接到工作 API 和不工作的 API。这是选项请求结果的并排屏幕截图。

调用两个 API 的选项请求

我经历了许多关于堆栈溢出的解决方案以及微软论坛。

我尝试将 kestrel 服务器行添加到 program.cs。

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((builderContext, config) =>
            {
  
            })
            .UseStartup<Startup>()
            .ConfigureKestrel((context, options) =>{}) // this line
            .UseNLog()
            .Build();
}

我在启动时添加了转发令牌,如下所示。

            services.AddAuthentication(IISDefaults.AuthenticationScheme)
                .AddJwtBearer(options => {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,

                        ValidIssuer = "<issuer removed>",
                        ValidAudience = "<audience removed>",
                        IssuerSigningKey = new SymmetricSecurityKey(
                                                Encoding.UTF8.GetBytes(Configuration["CapitalJobs:JWTSigningKey"]))
                    };

标签: c#asp.net.netangularsignalr

解决方案


推荐阅读