【问题标题】:How to enable Cors in Signalr Azure Service如何在 Signalr Azure 服务中启用 Cors
【发布时间】:2019-05-02 16:30:50
【问题描述】:

我正在尝试在仅包含集线器类的 Web 应用程序中使用 Azure SignalR 服务。当我尝试从另一个域访问集线器时,我收到以下错误

“从源“https://localhost:44303”访问“https://*/genericSocketHub/negotiate”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:没有“访问控制” -Allow-Origin' 标头出现在请求的资源上。”。

在我的项目的 startup.cs 类中,我有:

`公共类启动 { 公共 IConfiguration 配置 { 获取; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }


    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc()
            .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddCors(o => o.AddPolicy("Policy", builder =>
        {
            builder.AllowAnyOrigin()
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials();
        }));
        services.AddSignalR().AddAzureSignalR(Configuration.GetConnectionString("AzureSignalRConnectionString")).AddJsonProtocol(options => options.PayloadSerializerSettings = new Newtonsoft.Json.JsonSerializerSettings() { ContractResolver = new DefaultContractResolver()});
        services.AddSingleton(Configuration);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCors("Policy");
        app.UseAzureSignalR(routes =>
        {
            routes.MapHub<GenericSocketHub>("/genericSocketHub");
        });
        app.UseMvc();

    }
}`

没有使用 Azure SignalR 服务,我没有任何 CORS 问题

【问题讨论】:

    标签: c# asp.net-core signalr signalr-hub asp.net-core-signalr


    【解决方案1】:

    尝试将.WithOrigins("[THE_DOMAIN_TO_UNBLOCK]"); 添加到您的策略中:

    services.AddCors(o => o.AddPolicy("Policy", builder =>
        {
            builder.AllowAnyOrigin()
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials()
                .WithOrigins("[THE_DOMAIN_TO_UNBLOCK]");
        }));
    

    同时确保您在服务器上安装了最新版本的Microsoft.Asure.SignalR,同时在客户端上安装了最新版本的@aspnet/signalr

    注意 signalr npm 包与 Azure SignalR 不兼容。我很难学到这一点..

    以下适用于我的设置,即 Angular7、.NET CORE 2.1 和 Azure SignalR。我的设置如下所示:

    配置服务

    // Add CORS
      services.AddCors(options =>
      {
        options.AddPolicy("AllowAllOrigins",
                  builder =>
                  {
                    builder
                      .AllowAnyOrigin()
                      .AllowAnyHeader()
                      .AllowCredentials()
                      .AllowAnyMethod()
                      .WithOrigins("http://localhost:4200");
                  });
      });
    
      // Add Azure SignalR
      services.AddSignalR().AddAzureSignalR();
    

    配置

    app.UseCors("AllowAllOrigins");
    
    app.UseAzureSignalR(routes =>
    {
      routes.MapHub<NextMatchHub>("/nextmatch");
    });
    
    app.UseMvc(routes =>
    {
      routes.MapRoute(
                name: "default",
                template: "api/{controller=Home}/{action=Index}/{id?}");
    });
    

    注意 确保以与上面示例相同的顺序添加各种实现。我无法解释为什么它对订单很敏感,但这也是我的一个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-11
      • 2019-12-24
      • 1970-01-01
      • 2021-03-08
      • 2014-07-30
      • 1970-01-01
      相关资源
      最近更新 更多