SignalR 跨域

问题描述 投票:0回答:1

我可以看到这个问题已经被问过很多次了。然而所有解决方案都已有多年历史,并且不适用于 DotNet 6.0。

我有一个 SignalR js 客户端。它是从服务器 A 渲染的。我的 signalR 服务器位于服务器 B 上。它位于固定的 IP 地址上。

我尝试从客户端调用该服务器...

socket= new signalR.HubConnectionBuilder()
    .withUrl("http://X.X.X.X/MyHub")
    .configureLogging(signalR.LogLevel.Information)
    .build();

但是我收到 cors 错误:

错误:无法完成与服务器的协商:错误

我的服务器是 ubuntu 20。Web 服务器是用 asp.net C# ver 6.0 编写的

在服务器 B 中我有程序:

builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", policyBuilder =>
{
    policyBuilder
            .AllowAnyMethod()
            .AllowAnyHeader()
            .AllowAnyOrigin()
            .SetIsOriginAllowed(_ => true); // allow any origin 
}));

两台服务器都是 https。

在我的 nginx 配置文件中我有这个:

# Wide-open CORS config for nginx
    location / {
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-  Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
    if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-        Modified-Since,Cache-Control,Content-Type,Range' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
}

我从这里得到的:

https://enable-cors.org/server_nginx.html

c# nginx cors signalr asp.net-core-signalr
1个回答
0
投票

请尝试使用以下cors设置。

builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
{
    builder.AllowAnyMethod()
        .SetIsOriginAllowed(_ => true)
        .AllowAnyHeader()
        .AllowCredentials();
}));

原因

您同时使用

AllowAnyOrigin()
SetIsOriginAllowed(_ => true)
,如果在CORS配置中启用了凭据(例如cookie)并且您尝试使用
.AllowAnyOrigin()
方法,您将遇到跨域问题。

我知道我的示例代码不安全,如果它有效,您可以使用下面的代码。这样就安全了。

builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", policy =>
{
    policy
        .WithOrigins("Your A site") 
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials();
}));
© www.soinside.com 2019 - 2024. All rights reserved.