.NET Core 3.1 CORS 无法在 Ember (3.12) web UI 上运行
.NET Core 3.1 CORS not working on Ember (3.12) web UI
我正在将 .NET Core Web API 从 2.2 迁移到 3.1。我已按照 Microsoft 迁移说明进行操作,并将对 CORS 中间件的调用置于 UseRouting 和 UseEnpoints 之间。但是,我仍然收到 CORS 错误。
在 ConfigureServices 方法中,我有:
services.AddCors();
在配置方法中,我有:
app.UseRouting();
app.UseCors(options => options
.AllowAnyOrigin()
.AllowAnyHeader().AllowAnyMethod()
);
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Web 浏览器中的控制台错误是:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://localhost:5000/users/login. (Reason:
CORS header ‘Access-Control-Allow-Origin’ missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading
the remote resource at http://localhost:5000/users/login. (Reason:
CORS request did not succeed).
诊断:
我尝试遵循 Microsoft 的文档 Enable Cross-Origin Requests (CORS) in ASP.NET Core,但它使用了过时的引用,例如;使用 Mvc 和 IHostingEnvironment。
我尝试创建一个全新的 Web API 和 Ember Web-UI,但这也不起作用。你可以找到一个简单的例子getting-started-with-ember-js-and-net-core-3
知道导致问题的原因吗?
理想的赏金奖励将用于有效的答案。最好在 Git.
上发布一个有效的准系统项目
注意:我现在允许任何来源,这样我才能让它工作。最终,这将需要与 http://localhost:4200
上的 Web-UI 一起工作
更新
收到的状态代码是 HTTP 405 - 不允许的方法。所讨论的方法是一种 OPTIONS 方法。
.NET Core 2.2 到 3.0 的迁移文档是 incorrect。对 UseCors();
的调用需要放在 UseRouting()
函数之前。我继续将它添加到 UseHttpsRedirection()
之前,正如我在另一个站点的实施中看到的那样。
在配置服务中
services.AddCors(options =>
{
options.AddPolicy("CorsApiPolicy",
builder =>
{
builder.WithOrigins("http://localhost:4200")
.WithHeaders(new[] { "authorization", "content-type", "accept" })
.WithMethods(new[] { "GET", "POST", "PUT", "DELETE", "OPTIONS" })
;
});
});
在配置中
app.UseCors("CorsApiPolicy");
//app.UseHttpsRedirection();
app.UseJsonApi();
app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
我正在将 .NET Core Web API 从 2.2 迁移到 3.1。我已按照 Microsoft 迁移说明进行操作,并将对 CORS 中间件的调用置于 UseRouting 和 UseEnpoints 之间。但是,我仍然收到 CORS 错误。
在 ConfigureServices 方法中,我有:
services.AddCors();
在配置方法中,我有:
app.UseRouting();
app.UseCors(options => options
.AllowAnyOrigin()
.AllowAnyHeader().AllowAnyMethod()
);
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Web 浏览器中的控制台错误是:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/users/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:5000/users/login. (Reason: CORS request did not succeed).
诊断:
我尝试遵循 Microsoft 的文档 Enable Cross-Origin Requests (CORS) in ASP.NET Core,但它使用了过时的引用,例如;使用 Mvc 和 IHostingEnvironment。
我尝试创建一个全新的 Web API 和 Ember Web-UI,但这也不起作用。你可以找到一个简单的例子getting-started-with-ember-js-and-net-core-3
知道导致问题的原因吗?
理想的赏金奖励将用于有效的答案。最好在 Git.
上发布一个有效的准系统项目注意:我现在允许任何来源,这样我才能让它工作。最终,这将需要与 http://localhost:4200
更新
收到的状态代码是 HTTP 405 - 不允许的方法。所讨论的方法是一种 OPTIONS 方法。
.NET Core 2.2 到 3.0 的迁移文档是 incorrect。对 UseCors();
的调用需要放在 UseRouting()
函数之前。我继续将它添加到 UseHttpsRedirection()
之前,正如我在另一个站点的实施中看到的那样。
在配置服务中
services.AddCors(options =>
{
options.AddPolicy("CorsApiPolicy",
builder =>
{
builder.WithOrigins("http://localhost:4200")
.WithHeaders(new[] { "authorization", "content-type", "accept" })
.WithMethods(new[] { "GET", "POST", "PUT", "DELETE", "OPTIONS" })
;
});
});
在配置中
app.UseCors("CorsApiPolicy");
//app.UseHttpsRedirection();
app.UseJsonApi();
app.UseRouting();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});