Сигнал г не работает после обновления до .net core 2.2 - PullRequest
0 голосов
/ 07 января 2019

Мы используем сигнал r для запуска нашего интерфейса для обновлений, и он работал на 100% до тех пор, пока мы не обновились до .net core 2.2

См. Запуск ниже, методы вызывают, и нормальные вызовы на стороне сервера работают без проблем, но как только Сигнал r пытается соединиться с интерфейсом, я получаю ошибку cors.

Я пытался просто пропустить все сердечные сокращения, но это тоже не сработало.

protected IServiceProvider ConfigureServicesImplementation(IServiceCollection services)
    {
        services.Configure<DatabaseConfiguration>(Configuration.GetSection(DatabaseConfigurationName));
        services.Configure<TokenProviderConfiguration>(Configuration.GetSection(TokenProviderConfigurationName));
        services.AddOptions();
        services.AddCors();
        services.AddAutoMapper();
        services.AddAutofac();

        services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

        services.AddScoped<DatabaseMigrator>();
        services.AddScoped<AdminTenantDbInitialiser>();
        services.AddScoped<TenantDbInitialiser>();
        services.AddScoped<HubBase>();
        services.AddScoped<HubService>();

        services.AddSignalR()
        .AddJsonProtocol(options =>
            {
                options.PayloadSerializerSettings.Converters.Add(new StringEnumConverter
                {

                });
            });

        services.AddDbContext<DefaultDbContext>(options =>
        {
            options.UseSqlServer(Configuration.GetSection(DatabaseConfigurationName)["ConnectionString"]);
        });
        services.AddDbContext<TenantDbContext>(options =>
        {
            options.UseSqlServer(Configuration.GetSection(DatabaseConfigurationName)["TenantManagementConnectionString"]);
        });

        services.AddMemoryCache();
        services.AddHealthChecks();

        ConfigureAuthImplementation(services);
        ConfigureMvcImplementation(services);

        var builder = new ContainerBuilder();

        builder.RegisterAssemblyTypes(Assembly.Load("Digitise.Domain.Services"))
                      .Where(t => t.Name.EndsWith("Service", StringComparison.OrdinalIgnoreCase))
                      //.InstancePerRequest()
                      .PropertiesAutowired();

        builder.Populate(services);

        var container = builder.Build();

        return new AutofacServiceProvider(container);
    }

    private void ConfigureMvcImplementation(IServiceCollection services)
    {
        var mvc = services.AddMvcCore(options =>
        {
            options.Filters.Add(new ProducesAttribute("application/json"));
            options.Filters.Add(typeof(ValidateModelAttribute));
        })
            .AddApiExplorer()
            .AddAuthorization()
            .AddDataAnnotations()
            .AddJsonFormatters()
            .AddControllersAsServices()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.Converters.Add(new StringEnumConverter
                {

                });
            });

        if (Environment.IsDevelopment())
        {
            var controllers = Assembly.GetExecutingAssembly().GetTypes()
            .Where(type => typeof(ControllerBase).IsAssignableFrom(type))
            .ToList();

            var sp = services.BuildServiceProvider();
            foreach (var controllerType in controllers)
            {
                var controller = sp.GetService(controllerType);
            }
        }

        mvc.AddFluentValidation(fv =>
        {
            fv.RegisterValidatorsFromAssemblyContaining<UserLoginInputValidator>();
            fv.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
            fv.ImplicitlyValidateChildProperties = true;
        });
    }

    protected void ConfigureApp(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, IMapper autoMapper, bool addTenantRoute = false)
    {

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseSwaggerUi3(x => { });
            //autoMapper.ConfigurationProvider.AssertConfigurationIsValid();
        }
        else
        {
            app.UseExceptionHandler("/error");
            app.UseHsts();
            app.UseHttpsRedirection();
        }

        app.UseCors(builder => builder
           .AllowAnyOrigin()
           .AllowAnyMethod()
           .AllowAnyHeader()
           .AllowCredentials());

        app.UseSignalR(routes =>
        {
            routes.MapHub<HubBase>("/globalhub");
        });

        app.UseMvc(routes =>
        {
            if (addTenantRoute)
            {
                routes.MapRoute("default", "{controller}/{action}/{tenant?}/{id?}", defaults: new
                {
                    controller = "app",
                    action = "ping"
                });
            }
            else
            {
                routes.MapRoute("default", "{controller}/{action}/{id?}", defaults: new
                {
                    controller = "app",
                    action = "ping"
                });
            }
            routes.MapRoute(name: "ping", template: "app/ping", defaults: new { controller = "App", action = "Ping" });
        });

        app.UseAuthentication();
    }

request

Response error

что касается подключения с внешнего интерфейса, см. Код ниже.

public startHubConnection(): void {
    if (!this.hubConnection) {
        const currentUser = this.authenticationService.getCurrentUser();
        this.hubConnection = new HubConnectionBuilder().withUrl(this.url + '/globalhub?Authorization=Bearer ' + currentUser.accessToken).build();
    }

    this.hubConnection
        .start()
        .then(() => {
            this.isHubConnected = true;
            if (this.firstRun) {
                this.firstRun = false;
                this.manageHubConnections();
            }

            this.hubReconnectAllowed = true;
            this.serverStatusChange.emit(true);
            if (this.hubReconnectionId) {
                this.isHubConnected = true;
                this.notificationService.success('Server online', 'Reconnect with server successfull.');
                this.globalService.systemOnline = true;
                clearInterval(this.hubReconnectionId);
            }
        })
        .catch(err => console.dir('manual: ' + err));
}

Ответы [ 2 ]

0 голосов
/ 07 января 2019

Таким образом, в 2.2 .AllowAnyOrigin () + .AllowCredentials () больше не разрешено, поскольку это небезопасно. Вам необходимо явно установить разрешенные источники, используя WithOrigins (). Проблема решена благодаря BrennanConroy

Статья

0 голосов
/ 07 января 2019

CORS Middleware должно предшествовать любым определенным конечным точкам в вашем приложении, где вы хотите поддерживать запросы из разных источников, поэтому вы должны вызывать UseCors до UseSignalR:

...

app.UseCors(builder => builder
   .AllowAnyOrigin()
   .AllowAnyMethod()
   .AllowAnyHeader()
   .AllowCredentials());

app.UseSignalR(routes =>
{
    routes.MapHub<HubBase>("/globalhub");
});

...
...