我已将我的项目更新到 .net core 2.2,但似乎CORS在2.1版本中不存在的问题现在出现了。
我的应用程序运行在此URL上:http://*:5300
我在Startup.cs
中添加了以下代码:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddCors(options =>
options.AddPolicy("MyPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader();
}));
services.AddMvc();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...
app.UseCors(builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader();
});
app.UseAuthentication();
app.UseMvc();
}
这个方法没有起作用,所以我在我的“BaseController”类上添加了[EnableCors]
属性:
[EnableCors]
[Authorize]
[Produces("application/json")]
[Route("api/[controller]")]
public class BaseController : Controller
{
}
但我仍然遇到CORS错误:
从源'http://192.168.15.63:5302'访问'http://192.168.15.63:5301/api/permissions/UI'被CORS策略阻止:
预检请求的响应未能通过访问控制检查:
在响应中,'Access-Control-Allow-Origin'头的值在请求的凭据模式为“include”时不能是通配符'*'。
XMLHttpRequest发起的请求的凭证模式由withCredentials属性控制。
还有什么其他方法可以完全消除CORS?
http://*:5300
。 - Liran Friedman