[英]Angular and Asp.net core CORS issue
这个问题我已经有一段时间了,我已经尝试了所有可能的解决方案,我的 asp.net 核心服务器启用了 CORS,代码如下,但有时我会收到 No 'Access-Control-Allow-Origin' 的错误
我的生产 API 托管在 windows 服务器上的 IIS 上
有什么办法可以调试这个吗?
CORS 策略已阻止从源“DOMAIN”访问“Production_API”处的 XMLHttpRequest:请求的资源上不存在“Access-Control-Allow-Origin”Z099FB995346F31C749F6E40DB0F395。 zone.js:3372
获取 APICALL 网络::ERR_FAILED
在 zone.js 中,它说它有一个错误“加载资源失败:net::ERR_FAILED”
sendNative.apply(target, data.args);
target[XHR_SCHEDULED] = true;
return task;
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);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper _mapper = mappingConfig.CreateMapper();
services.AddSingleton(_mapper);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("AllowEverything");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
//app.UseHsts();
}
app.UseCors("AllowEverything");
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
AllowAnyOrigin()
仅在不使用AllowCredentials()
时有效
您必须使用.WithOrigins("http://example.com","http://www.contoso.com");
使用凭据时。
你也使用app.UseCors(AllowEverything);
在你的
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
我尝试了很多不同的解决方案,最后我发现 http 拦截器上有一个未知错误,我认为当系统在 CPU 使用率达到最大值时会发生这种情况,因为 angular 无法完成调用和它进入未知错误,因为 CPU 没有处理它的空间。 请让我知道这是否有意义或在繁重的应用程序中可能发生的事情。
谢谢
尝试 x 是策略:
app.UseCors(x => x.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:****"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.