添加指定跨域支持
private const string DefaultCorsPolicyName = "Default"; public override void ConfigureServices(ServiceConfigurationContext context) { var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); //............................ //添加跨域支持:原有代码不用动 context.Services.AddCors(options => { options.AddPolicy(DefaultCorsPolicyName , builder => { builder.WithOrigins( configuration["App:CorsOrigins"] .Split(",", StringSplitOptions.RemoveEmptyEntries) .Select(o => o.RemovePostFix("/")) .ToArray() ) .WithAbpExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); //必须放在下面2个的前面才有用 不然还是报跨域错误: app.UseCors(DefaultCorsPolicyName); app.UseIdentityServer(); app.UseAuthorization(); }
添加所有跨域支持:简单粗暴疗效好
private const string DefaultCorsPolicyName = "Default"; public override void ConfigureServices(ServiceConfigurationContext context) { var hostingEnvironment = context.Services.GetHostingEnvironment(); var configuration = context.Services.GetConfiguration(); //............................ //添加跨域支持:原有代码不用动 context.Services.AddCors(options => { options.AddPolicy(DefaultCorsPolicyName , builder => { builder .AllowAnyOrigin()//添加这个 .WithAbpExposedHeaders() .SetIsOriginAllowedToAllowWildcardSubdomains() .AllowAnyHeader() .AllowAnyMethod(); //.AllowCredentials(); 去掉这个 }); }); } public override void OnApplicationInitialization(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); //必须放在下面2个的前面才有用 不然还是报跨域错误: app.UseCors(DefaultCorsPolicyName); app.UseIdentityServer(); app.UseAuthorization(); }