.NET Core 2.2 CORS不允许请求

.NET Core 2.2 CORS不允许请求,.net,asp.net-mvc,asp.net-core,cors,.net,Asp.net Mvc,Asp.net Core,Cors,我已经检查了这方面的其他几个线程,仍然无法设法解决这个问题。我想允许任何来源、头、方法等访问我的.NETCore2.2API public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddMvc(); ... public void Configure(IApplicationBuilder app, IHostingEnvi

我已经检查了这方面的其他几个线程,仍然无法设法解决这个问题。我想允许任何来源、头、方法等访问我的.NETCore2.2API

public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors();
        services.AddMvc();  
... 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, IDistributedCache cache)
    {
        app.UseCors(builder => builder.AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader()
        .AllowCredentials());
我确保首先在ConfigureServices和Configure中调用CORS方法。我在本地和服务器上测试这个问题,在Chrome上都出现了同样的错误

Access to XMLHttpRequest at 'https://xxxxx.azurewebsites.net/api/Employees/getCurrentEmployee' from origin 'https://xxxxxxxxxx.azurewebsites.net' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
您可以“手动”完成,通过这种方式,添加额外的逻辑来管理您的请求非常简单。 在您的global.asax中,添加一个带有请求域的自定义标头,以便允许发件人,或者为您希望的特定域添加自定义标头,或者只添加一个通配符“*”,它允许所有发件人。请注意,chrome会使用通配符发出安全警告,因此应避免使用通配符

.AddCustomHeader("Access-Control-Allow-Origin",  HttpContext.Current.Request.UserHostAddress)

您可以尝试改用策略:

public void ConfigureServices(IServiceCollection services)
{
    services.AddCors(setup => setup.AddPolicy("CorsPolicy", builder =>
        {
            builder.AllowAnyOrigins()
            .AllowAnyHeader()
            .AllowAnyMethod()
            .AllowCredentials();
        }));

如果AllowAnyOrigins()不起作用,您可以始终指定url并将其写入
。WithOrigins(“”

所以应该是:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(setup => setup.AddPolicy("CorsPolicy", builder =>
            {
                builder.WithOrigins("https://xxxxxxxxxx.azurewebsites.net")
                .AllowAnyHeader()
                .AllowAnyMethod()
                .AllowCredentials();
            }));
... 

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime lifetime, IDistributedCache cache)
        {
            app.UseCors("CorsPolicy");

在ConfigureServices方法中

services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder
                    .SetIsOriginAllowed((host) => true)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });
在配置方法中

app.UseCors("CorsPolicy");

我认为这实际上更多的是因为我的应用程序没有正确地发布到应用程序服务,因此没有反映上面的代码。啊

以以下方式允许.Net Core 2.2中的CORS请求:

// This method gets called by the runtime. Add/allow tags are per your needs
public void ConfigureServices(IServiceCollection services)
{
  services.AddCors(options =>
   {
      options.AddPolicy("AllowAllOrigins",
      builder => builder.AllowAnyOrigin()
      .AllowAnyMethod()
     .AllowAnyHeader()
     );
   });
}

谢谢

但是你没有AllowCredentials,所以Cookie不允许添加/允许标记是根据应用程序的需要。它不是这样工作的:.net core 2.2禁止AllowOrigin和AllowCredentials一起使用。有关@Thomas评论的参考:
// This method gets called by the runtime. Add/allow tags are per your needs
public void ConfigureServices(IServiceCollection services)
{
  services.AddCors(options =>
   {
      options.AddPolicy("AllowAllOrigins",
      builder => builder.AllowAnyOrigin()
      .AllowAnyMethod()
     .AllowAnyHeader()
     );
   });
}