Angular和Asp.net核心CORS问题

Angular和Asp.net核心CORS问题,angular,asp.net-core,Angular,Asp.net Core,我有这个问题已经有一段时间了,我已经尝试了所有可能的解决方案,我的asp.net核心服务器启用了CORS,代码如下,但有时我会出现“无访问控制允许来源”的错误 我的生产API托管在windows server上的IIS上 有什么办法可以调试这个吗 CORS策略已阻止从源“域”访问“Production_API”处的XMLHttpRequest:请求的资源上不存在“Access Control Allow origin”标头。 zone.js:3372 获取APICALL网络::错误\u失败 s

我有这个问题已经有一段时间了,我已经尝试了所有可能的解决方案,我的asp.net核心服务器启用了CORS,代码如下,但有时我会出现“无访问控制允许来源”的错误

我的生产API托管在windows server上的IIS上

有什么办法可以调试这个吗

CORS策略已阻止从源“域”访问“Production_API”处的XMLHttpRequest:请求的资源上不存在“Access Control Allow origin”标头。 zone.js:3372

获取APICALL网络::错误\u失败

 sendNative.apply(target, data.args);
 target[XHR_SCHEDULED] = true;
 return task;
在zone.js中,它说它有一个错误“加载资源失败:net::ERR_Failed”

ASP.net核心CORS代码

    public void ConfigureServices(IServiceCollection services)
    {
       

        services.AddCors(options =>
        {
            options.AddPolicy("AllowEverything",
                builder => builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());
        });

        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        services.AddDbContext<ApplicationDbContext>(options =>
           options.UseSqlServer(
               Configuration.GetConnectionString("DefaultConnection")));

        

        services.AddIdentity<ApplicationUser, IdentityRole>(config =>
        {
            config.SignIn.RequireConfirmedEmail = false;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddRoleManager<RoleManager<IdentityRole>>()
        .AddDefaultUI()
        .AddDefaultTokenProviders();

        services.AddAuthentication(
          option =>
          {
              option.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
              option.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
              option.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
          })
       .AddJwtBearer(options =>
       {
           options.TokenValidationParameters.ValidateIssuer = true;
           options.TokenValidationParameters.ValidateIssuerSigningKey = true;
           options.TokenValidationParameters.ValidateLifetime = true;
           options.TokenValidationParameters.IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Tokens:Key"]));
           options.TokenValidationParameters.ValidIssuer = Configuration["Tokens:Issuer"];
           options.TokenValidationParameters.ValidAudience = Configuration["Tokens:Audience"];
       });
    

       

        services.AddTransient<Services.IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
        services.Configure<SMSoptions>(Configuration);

        
AllowAnyOrigin()
仅在不使用
AllowCredentials()时有效。

您必须使用
.WithOrigins(“http://example.com","http://www.contoso.com");使用凭据时

你也使用app.UseCors(AllowEverything)吗在您的

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

我尝试了很多不同的解决方案,最后我发现http拦截器上有一个未知错误,我认为当系统在CPU使用量达到最大值时,就会发生这种情况,因为angular无法完成调用,它会出现未知错误,因为CPU没有空间处理它。 请让我知道这是否有意义,或者重型应用程序可能发生的情况


谢谢你

在你的问题中,我缺少app.UseCors(…)
的中间件配置。这很重要,因为宣布中间产品的顺序就是处理它们的顺序。通常,异常处理(页面)在cors中间件之前声明,这会导致错误响应缺少cors头;你能告诉我们你还有哪些其他的中间件,它们的顺序和你在代码中的顺序完全一样吗?我没有使用任何其他的中间件!那么您的应用程序是如何工作的?我很想知道为什么你会使用
AddMvc()
,但是不要为它添加中间件,比如
app.UseRouting()
app.UseMvc()
app.UseAuthorization()
.app.UseCors(“Allowerything”);是的,我正在使用它好的,所以如果我去掉所有的齿槽,它会解决这个问题吗?非常感谢。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)