nginx SignalR跨域

ldfqzlk8  于 4个月前  发布在  Nginx
关注(0)|答案(1)|浏览(48)

我可以看到这个问题已经被问过很多次了。但是所有的解决方案都是几年前的,并且不适用于DotNet 6.0。
我有一个SignalR js客户端。它从服务器A呈现。我在服务器B上有我的signalR服务器。它在一个固定的IP地址上。
我试着从客户端调用这个服务器...

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

字符串
但我得到了错误:


的数据
错误:无法完成与服务器的协商:错误
我的服务器是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

upstream websocketCloud {
  server 127.0.0.1:9771;
}

w1jd8yoj

w1jd8yoj1#

请尝试使用以下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();
}));

相关问题