【发布时间】:2019-05-24 17:39:47
【问题描述】:
我已将我的项目更新到 .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”的 XMLHttpRequest 已被 CORS 策略阻止:
对预检请求的响应未通过访问控制检查:
当请求的凭据模式为“包含”时,响应中的“Access-Control-Allow-Origin”标头的值不能是通配符“*”。
XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。
我还能做些什么来完全删除 CORS?
【问题讨论】:
-
你在用什么浏览器..?它看起来像 this issue.. 一个安全功能..
-
我正在使用 Chrome,顺便说一句,我补充说我正在运行这样的应用程序:
http://*:5300
标签: c# asp.net-core asp.net-core-2.2