。NET Core 3.1 CORS在Ember(3.12)Web UI上不起作用

问题描述 投票:0回答:1

我正在将.NET Core Web API从2.2迁移到3.1。我已经按照Microsoft的迁移说明进行了操作,并在UseRouting和UseEnpoints之间放置了对CORS中间件的调用。但是,我仍然收到CORS错误。

在ConfigureServices方法中,我具有:

services.AddCors();

在Configure方法中,我有:

app.UseRouting();

app.UseCors(options => options
        .AllowAnyOrigin()
        .AllowAnyHeader().AllowAnyMethod()
    );

app.UseAuthentication();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

Web浏览器中的控制台错误是:

跨域请求被阻止:同源政策不允许阅读http://localhost:5000/users/login处的远程资源。 (原因:CORS标头“ Access-Control-Allow-Origin”丢失)。

跨域请求被阻止:同源政策不允许阅读http://localhost:5000/users/login处的远程资源。 (原因:CORS请求未成功)。

诊断:

我尝试遵循Microsoft的文档Enable Cross-Origin Requests (CORS) in ASP.NET Core,但是它使用了过时的引用,例如; UseMvc和IHostingEnvironment。

我曾尝试创建一个全新的Web API和Ember Web-UI,但这也不起作用。您可以找到一个简单的示例getting-started-with-ember-js-and-net-core-3

任何想法导致问题的原因?

理想的赏金奖励将用于有效的答案。最好在Git上发布一个正在工作的准系统项目。

注意:我现在允许任何来源,以便使其正常工作。最终,这将需要使用http://localhost:4200

上的Web UI

更新

收到的状态码是HTTP 405-不允许的方法。所讨论的方法是OPTIONS方法。

ember.js cors cross-domain ember-data asp.net-core-3.1
1个回答
0
投票

。NET Core 2.2到3.0的迁移文档不正确。对UseCors();的调用需要放在UseRouting()功能之前。我继续将其添加到UseHttpsRedirection()之前,就像在另一个站点的实现中看到的那样。

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio

在ConfigureServices中]

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("ApiCorsPolicy");

//app.UseHttpsRedirection(); 

app.UseJsonApi();

app.UseRouting();

app.UseAuthentication();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});
© www.soinside.com 2019 - 2024. All rights reserved.