CORS 问题 .NetCore3 web api,对预检请求的响应未通过访问控制检查

CORS problem .NetCore3 web api,Response to preflight request doesn't pass access control check

NetCore 3.0 和 Angular 在我的本地主机上都是 运行, 它是一个身份验证项目,我将我的启动设置为:

 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.AddAuthentication(opt => {
            opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddJwtBearer(options =>
        {
            options.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,

                ValidIssuer = "https://localhost:44361",
                ValidAudience = "https://localhost:4200",
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("superSecretKey@345"))
            };
        });

        services.AddCors(options =>
        {
            options.AddPolicy("EnableCORS", builder =>
            {
                builder.WithOrigins("http://localhost:4200")
                .AllowAnyHeader()
                .AllowAnyMethod();
            });
        });

        services.AddControllers();
    }

    // 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.UseHttpsRedirection();
        app.UseCors("EnableCORS");

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

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

我的 Angular 在端口 localhost:4200 上 运行 而我的 API 在 localhost:44361

上运行

知道为什么我会收到那个错误吗?很奇怪似乎一切都很好应该没有那么复杂,或者我遗漏了什么?我已经处理了 3 天仍然没有成功

With endpoint routing, the CORS middleware must be configured to execute between the calls to UseRouting and UseEndpoints.

您可以在 Microsoft docs 上的启用跨源请求文档中阅读此内容。

移动您的 app.UseCors(),使其位于路由和端点中间件之间。

        app.UseRouting();
        app.UseCors("EnableCORS");

        app.UseAuthentication();
        app.UseAuthorization();

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