我可以看到这个问题已经被问了很多次了.然而,所有的解决方案都是老旧的,不能在DotNet 6.0上运行.

我有一个SignalR js客户.它是从服务器A呈现的.我的SignalR服务器在服务器B上.它位于固定的IP地址上.

我try 从客户端调用此服务器...

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

但我得到了错误:

enter image description here

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

我的服务器是ubuntu 20.Web服务器是用ASP.NET C#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;
}

推荐答案

请try 使用以下CORS设置.

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

Reason

您同时使用AllowAnyOrigin()SetIsOriginAllowed(_ => true),如果在CORS配置中启用了凭据(例如Cookie),并且您try 使用.AllowAnyOrigin()方法,则会遇到跨域问题.

我知道我的示例代码不安全,如果它有效,你可以使用下面的代码.它会很安全的.

builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", policy =>
{
    policy
        .WithOrigins("Your A site") 
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials();
}));

Csharp相关问答推荐

需要更改哪些内容才能修复被覆盖的财产中的无效警告CS 8765?

MongoDB实体框架核心:表达必须可写

EF Core判断是否应用了AsSplitQuery()

有没有一种方法可以防止在编译时在MicrosoftC或非单线程上下文中调用方法?

一种安全的方式来存储SSH凭证(MAUI/C#应用程序)

如何使用CsvReader获取给定列索引的列标题?

.NET HttpClient、JsonSerializer或误用的Stream中的内存泄漏?

HttpConext.Request.Path和HttpConext.GetEndpoint()之间的差异

为具有实体框架后端的Reaction项目 Select 正确的Visual Studio模板

如何将字符串变量传递给JObject C#-无法加载文件或程序集';System.Text.Json

如何比较C#中的L和ł(波兰字符)返回TRUE

使用ASP.NET MVC for Lemon Squeezy X-Signature创建散列

序列化过程中的死循环

并发表更新.EF核心交易

SignalR跨域

在Visual Studio 2022中查找Xamarin模板时遇到问题

最小API定义的Swagger标头参数

是否在异步方法中避免Span<;T>;.ToArray()?

无法创建工具窗口(用于VBIDE、VBA的COM加载项扩展)

ASP.NET重新加载Kestrel SSL证书