加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在Signalr Azure Service中启用Cors

发布时间:2020-12-15 22:53:53 所属栏目:百科 来源:网络整理
导读:我试图在仅包含集线器类的Web应用程序中使用Azure SignalR服务.当我尝试从另一个域访问集线器时,我收到以下错误 “来自’https://localhost:44303‘的’https:// * / genericSocketHub / negotiate’访问XMLHttpRequest已被CORS策略阻止:对预检请求的响应
我试图在仅包含集线器类的Web应用程序中使用Azure SignalR服务.当我尝试从另一个域访问集线器时,我收到以下错误

“来自’https://localhost:44303‘的’https:// * / genericSocketHub / negotiate’访问XMLHttpRequest已被CORS策略阻止:对预检请求的响应未通过访问控制检查:否’Access-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?}");
});

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读