CORS 在两个项目和用户之间通过 javascript
CORS Between two projects and users via javascript
我有两个申请:
.NET Core 2.0 中的 WebAPI
.NET Core 2.0 中的前端 MVC 使用 javascript 发送 post 请求。
出于某些原因,Firefox 仅 "accepts" 第一个请求,其余请求由于 CORS 问题而被拒绝,同时 Chrome 更接近 50/50 - 一些请求通过并且 return 后台数据正常,部分数据被屏蔽
此外,所有内容都在具有 2x 端口转发和 2x 反向代理的虚拟机中
后端
Host: HOST_IP:55555 is forwarded to GUESTIP:80
前端
Host: HOST_IP:44444 is forwarded to GUESTIP:44445
然后在 vm nginx 代理里面
port 80 to: http://localhost:55659 (backend app)
port 22222 to http://localhost:63382 (frontend app)
前端在浏览器中正确呈现,后端可通过 postman 和 returns 数据正确访问,有时请求在 chrome.
中工作
POST http://10.1.14.142:55555/test/test 400 (Bad Request)
vue-resource.js:1088 POST http://10.1.14.142:55555/test/test 400 (Bad Request)
:55555/test/test:1 POST http://10.1.14.142:55555/test/test 503 (Service Temporarily Unavailable)
test:1 Failed to load http://10.1.14.142:55555//test/test: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.1.14.142:44444' is therefore not allowed access. The response had HTTP status code 503.
Cross-Origin Read Blocking (CORB) blocked cross-origin response http://10.1.14.142:55555//test/test with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
vue-resource.js:1088 POST http://10.1.14.142:55555//test/test 400 (Bad Request)
如您所见,由于发送空表单
,某些请求已到达后端并return编辑BadRequest
这是我一直在尝试的一切:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAll",
p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
jwt...
services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors("AllowAll");
app.UseMvc();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
jwt...
services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
}
with:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors(builder => builder
.WithOrigins("http://10.1.14.142:44444"));
app.UseMvc();
}
or
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors(builder => builder
.WithOrigins("http://10.1.14.142:44444")
.AllowAnyHeader()
.AllowAnyMethod());
app.UseMvc();
}
or
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors(builder => builder
.WithOrigins("http://10.1.14.142:44444")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
app.UseMvc();
}
有什么想法吗?提前致谢。
解决方案:
正在禁用 NGINX 的速率限制器。
妈的,我浪费了这么多时间。我希望这是一个实际的解决方案,它不会在接下来的 1 小时内恢复...
这里是startup.cs:
app.UseCors
(
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
我有两个申请:
.NET Core 2.0 中的 WebAPI
.NET Core 2.0 中的前端 MVC 使用 javascript 发送 post 请求。
出于某些原因,Firefox 仅 "accepts" 第一个请求,其余请求由于 CORS 问题而被拒绝,同时 Chrome 更接近 50/50 - 一些请求通过并且 return 后台数据正常,部分数据被屏蔽
此外,所有内容都在具有 2x 端口转发和 2x 反向代理的虚拟机中
后端
Host: HOST_IP:55555 is forwarded to GUESTIP:80
前端
Host: HOST_IP:44444 is forwarded to GUESTIP:44445
然后在 vm nginx 代理里面
port 80 to: http://localhost:55659 (backend app)
port 22222 to http://localhost:63382 (frontend app)
前端在浏览器中正确呈现,后端可通过 postman 和 returns 数据正确访问,有时请求在 chrome.
中工作POST http://10.1.14.142:55555/test/test 400 (Bad Request)
vue-resource.js:1088 POST http://10.1.14.142:55555/test/test 400 (Bad Request)
:55555/test/test:1 POST http://10.1.14.142:55555/test/test 503 (Service Temporarily Unavailable)
test:1 Failed to load http://10.1.14.142:55555//test/test: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://10.1.14.142:44444' is therefore not allowed access. The response had HTTP status code 503.
Cross-Origin Read Blocking (CORB) blocked cross-origin response http://10.1.14.142:55555//test/test with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
vue-resource.js:1088 POST http://10.1.14.142:55555//test/test 400 (Bad Request)
如您所见,由于发送空表单
,某些请求已到达后端并return编辑BadRequest
这是我一直在尝试的一切:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAll",
p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
jwt...
services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors("AllowAll");
app.UseMvc();
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
jwt...
services
.AddMvc()
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssemblyContaining<Startup>());
}
with:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors(builder => builder
.WithOrigins("http://10.1.14.142:44444"));
app.UseMvc();
}
or
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors(builder => builder
.WithOrigins("http://10.1.14.142:44444")
.AllowAnyHeader()
.AllowAnyMethod());
app.UseMvc();
}
or
public void Configure(IApplicationBuilder app, IHostingEnvironment env, Persistance.AppContext context)
{
app.UseDeveloperExceptionPage();
app.UseAuthentication();
app.UseCors(builder => builder
.WithOrigins("http://10.1.14.142:44444")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
app.UseMvc();
}
有什么想法吗?提前致谢。
解决方案:
正在禁用 NGINX 的速率限制器。
妈的,我浪费了这么多时间。我希望这是一个实际的解决方案,它不会在接下来的 1 小时内恢复...
这里是startup.cs:
app.UseCors
(
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);