使用Angular 14时,CORS Preflight无法与SignalR和IIS一起工作

ttcibm8c  于 2023-08-05  发布在  Angular
关注(0)|答案(1)|浏览(138)

我尝试与SignalR建立连接,但收到以下错误:

Access to fetch at 'http://SAME.NETWORK:SOMEPORT/PortalCommunicationsHub/negotiate?negotiateVersion=1' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

字符串
这是我的服务代码:

@Injectable({
  providedIn: 'root'
})
export class SignalRService {
  public data: dataProfile[];

  private hubConnection: signalR.HubConnection;

  public startConnection = () => {
    this.hubConnection = new signalR.HubConnectionBuilder()
    .withUrl('http://SAME.NETWORK:SOMEPORT/PortalCommunicationsHub')
    .build();

    this.hubConnection
    .start()
    .then(() => console.log('Connection started'))
    .catch(err => console.log('Error starting connection: ' + err));
  }

  public availableDoorProfilesListener = () => {
    this.hubConnection.on('availableDoorProfiles', (data) => {
      this.data = data;
      console.log(data);
    });
  }

  constructor() { }
}


我这样调用服务:

export class DoorProfileComponent implements OnInit {
  options: Option[];

  selectedOption: Option;

  constructor(public signalRService: SignalRService, private httpClient: HttpClient) {}

  ngOnInit() {
    this.signalRService.startConnection();
    this.signalRService.availableProfilesListener();
  }

}


服务器CORS设置:

public const string CORS_ALL = "AllCors";

            options.AddPolicy(name: CORS_ALL,
                    corsOptions =>
                        corsOptions
                            .WithOrigins("*"));

 

        app.UseCors(configuration["CorsPolicy"] ?? Startup.CORS_LOCALHOST);


似乎是正确的,但我得到了讨厌的CORS信息。
我很感激任何帮助。

wko9yo5t

wko9yo5t1#

它可能是您的策略名称:

app.UseCors(configuration["CorsPolicy"] ?? Startup.CORS_LOCALHOST);

字符串
从上面提供的代码中,您正在使用:“AllCors

相关问题