簡體   English   中英

如何在Signalr Azure Service中啟用Cors

[英]How to enable Cors in Signalr Azure Service

我試圖在僅包含集線器類的Web應用程序中使用Azure SignalR服務。 當我嘗試從另一個域訪問集線器時,我收到以下錯誤

“從原始' https:// localhost:44303 '訪問'https:// * / genericSocketHub / negotiate'的XMLHttpRequest已被CORS策略阻止:對預檢請求的響應未通過訪問控制檢查:否'訪問 - Control-Allow-Origin'標頭出現在請求的資源上。“

在我的項目的startup.cs類中,我有:

`public class Startup {public IConfiguration Configuration {get; }

    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問題

嘗試添加.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。 我的設置如下:

ConfigureServices

// 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?}");
});

注意確保以與上面示例相同的順序添加各種實現。 我無法解釋為什么它對訂單敏感,但這也是我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM