为什么 CORS 在 ASP.NET Core 3 Web Api 中不起作用?
Why doesn't CORS work in ASP.NET Core 3 Web Api?
我尝试基于 ASP.NET Core 3 编写简单的 Web Api。但是我对 CORS 策略有疑问。那是我的初创公司 class:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors(
options => options.WithOrigins("http://myorigin.com").AllowAnyMethod().AllowAnyHeader()
);
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
这就是我从前端调用它的方式:
function api_call()
{
var form = document.forms["userForm"];
$.ajax({
type: "GET",
url: "http://myorigin.com:8080/access",
contentType: "application/json",
crossDomain:true,
success: function (worker) {
form.elements["result"].value = worker;
},
error: function (jxqr, error, status) {
console.log(jxqr);
if(jxqr.responseText===""){
form.elements["result"].value = jxqr.statusText;
}
else{
var response = JSON.parse(jxqr.responseText);
console.log(response);
if (response['Auth']) {
$.each(response['Auth'], function (index, item) {
form.elements["result"].value = item;
});
}
}
},
});
}
但是此函数无法访问 API。它 returns 请求被 CORS 策略阻止的消息:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我尝试通过不同的文章以不同的方式使用 CORS。我也尝试添加手动 CORS 中间件,但结果是一样的。
我的网站 Api 由 kestrel 服务无误地托管在 Ubuntu 和 运行 上。此外,当我 运行 它在 Visual studio 本地并在我的 JS 函数路径中写入本地主机时(url:“http://localhost:56597/access”)它调用 API 并获取它的响应成功。
我的错误是什么?我的前端和 Web Api 位于同一主机上但监听不同的端口。
我犯了错误。我忘了在我的 Configure 方法中添加这个:
app.UseAuthentication();
我在一篇文章中读到,我的情况需要它。
我的 Apache Web 配置也有错误 Api。我有这样的参数:
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
但他们应该是这样的:
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
现在我的 Web Api 可以正常工作,并且 CORS 没有问题。
我尝试基于 ASP.NET Core 3 编写简单的 Web Api。但是我对 CORS 策略有疑问。那是我的初创公司 class:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseCors(
options => options.WithOrigins("http://myorigin.com").AllowAnyMethod().AllowAnyHeader()
);
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
这就是我从前端调用它的方式:
function api_call()
{
var form = document.forms["userForm"];
$.ajax({
type: "GET",
url: "http://myorigin.com:8080/access",
contentType: "application/json",
crossDomain:true,
success: function (worker) {
form.elements["result"].value = worker;
},
error: function (jxqr, error, status) {
console.log(jxqr);
if(jxqr.responseText===""){
form.elements["result"].value = jxqr.statusText;
}
else{
var response = JSON.parse(jxqr.responseText);
console.log(response);
if (response['Auth']) {
$.each(response['Auth'], function (index, item) {
form.elements["result"].value = item;
});
}
}
},
});
}
但是此函数无法访问 API。它 returns 请求被 CORS 策略阻止的消息:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我尝试通过不同的文章以不同的方式使用 CORS。我也尝试添加手动 CORS 中间件,但结果是一样的。 我的网站 Api 由 kestrel 服务无误地托管在 Ubuntu 和 运行 上。此外,当我 运行 它在 Visual studio 本地并在我的 JS 函数路径中写入本地主机时(url:“http://localhost:56597/access”)它调用 API 并获取它的响应成功。
我的错误是什么?我的前端和 Web Api 位于同一主机上但监听不同的端口。
我犯了错误。我忘了在我的 Configure 方法中添加这个:
app.UseAuthentication();
我在一篇文章中读到,我的情况需要它。 我的 Apache Web 配置也有错误 Api。我有这样的参数:
ProxyPass / http://localhost:5000/
ProxyPassReverse / http://localhost:5000/
但他们应该是这样的:
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
现在我的 Web Api 可以正常工作,并且 CORS 没有问题。