在我的应用程序中,2个项目和mvc客户端在端口(5002)上运行,而Web api项目在端口(5001)上运行.我已经在mvc客户端中实现了signalr.现在在控制台中显示错误日志,如下所示:
In my application 2 projects and mvc client run at port(5002) and web api project run at port (5001). I have implemented signalr in mvc client. Now showing error log in console as below:
而且我还为api策略添加了对我的api项目的配置,例如:
and i have also added configuration to my api project for core policy like:
services.AddCors(options => { options.AddPolicy("signalr", builder => { builder.WithOrigins("localhost:5002") .AllowAnyHeader() .AllowAnyMethod(); }); });现在也显示相同的错误.请提出建议.
And now also showing same error. Please suggest.
推荐答案您需要这样配置CORS:
You need to configure your CORS like this:
services.AddCors(options => { options.AddPolicy("signalr", builder => builder.WithOrigins("localhost:5002") .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials() .SetIsOriginAllowed((host) => true)); });您传递给 .SetIsOriginAllowed()方法的lambda函数在允许原点的情况下返回true,因此始终返回true允许任何原点将请求发送到api.使用此方法时返回的allow origin access control http标头包含发送请求的源,而不是通配符,例如Access-Control-Allow-Origin:localhost:4200.
The lambda function that you pass to the .SetIsOriginAllowed() method returns true if an origin is allowed, so always returning true allows any origin to send requests to the api. The allow origin access control http header returned when using this method contains the origin that sent the request, not a wildcard, e.g. Access-Control-Allow-Origin: localhost:4200.
更多推荐
ASP.NET Core中Signalr上的javascript xmlhttp错误
发布评论