我可以看到这个问题已经被问了很多次了.然而,所有的解决方案都是老旧的,不能在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();
但我得到了错误:
错误:无法完成与服务器的协商:错误
我的服务器是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;
}