我尝试与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信息。
我很感激任何帮助。
1条答案
按热度按时间wko9yo5t1#
它可能是您的策略名称:
字符串
从上面提供的代码中,您正在使用:“
AllCors
“