nginx SignalR跨域

ldfqzlk8  于 2024-01-06  发布在  Nginx
关注(0)|答案(1)|浏览(309)

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

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

字符串
但我得到了错误:


的数据
错误:无法完成与服务器的协商:错误
我的服务器是ubuntu 20。Web服务器是用asp.net C# ver 6.0编写的
在服务器B中,我有一个程序:

  1. builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", policyBuilder =>
  2. {
  3. policyBuilder
  4. .AllowAnyMethod()
  5. .AllowAnyHeader()
  6. .AllowAnyOrigin()
  7. .SetIsOriginAllowed(_ => true); // allow any origin
  8. }));


两个服务器都是https。
在我的nginx配置文件中,我有这个:

  1. # Wide-open CORS config for nginx
  2. location / {
  3. if ($request_method = 'OPTIONS') {
  4. add_header 'Access-Control-Allow-Origin' '*';
  5. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  6. #
  7. # Custom headers and headers various browsers *should* be OK with but aren't
  8. #
  9. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
  10. #
  11. # Tell client that this pre-flight info is valid for 20 days
  12. #
  13. add_header 'Access-Control-Max-Age' 1728000;
  14. add_header 'Content-Type' 'text/plain; charset=utf-8';
  15. add_header 'Content-Length' 0;
  16. return 204;
  17. }
  18. if ($request_method = 'POST') {
  19. add_header 'Access-Control-Allow-Origin' '*' always;
  20. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  21. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If- Modified-Since,Cache-Control,Content-Type,Range' always;
  22. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  23. }
  24. if ($request_method = 'GET') {
  25. add_header 'Access-Control-Allow-Origin' '*' always;
  26. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
  27. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If- Modified-Since,Cache-Control,Content-Type,Range' always;
  28. add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
  29. }
  30. }


我从这里得到的:
https://enable-cors.org/server_nginx.html

  1. upstream websocketCloud {
  2. server 127.0.0.1:9771;
  3. }

w1jd8yoj

w1jd8yoj1#

请尝试使用以下cors设置。

  1. builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", builder =>
  2. {
  3. builder.AllowAnyMethod()
  4. .SetIsOriginAllowed(_ => true)
  5. .AllowAnyHeader()
  6. .AllowCredentials();
  7. }));

字符串

理由

您正在同时使用AllowAnyOrigin()SetIsOriginAllowed(_ => true),如果在CORS配置中启用了凭据(如Cookie),并且您尝试使用.AllowAnyOrigin()方法,则会遇到跨域问题。
我知道我的示例代码不安全,如果它工作,你可以使用下面的代码。它将是安全的。

  1. builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", policy =>
  2. {
  3. policy
  4. .WithOrigins("Your A site")
  5. .AllowAnyMethod()
  6. .AllowAnyHeader()
  7. .AllowCredentials();
  8. }));

展开查看全部

相关问题