2 回答
TA贡献1887条经验 获得超5个赞
在ConfigureServices方法,定义了两个政策即CorsAllowAll与CorsAllowSpecific
services.AddCors(options =>
{
options.AddPolicy("CorsAllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
options.AddPolicy("CorsAllowSpecific",
p => p.WithHeaders("Content-Type","Accept","Auth-Token")
.WithMethods("POST","PUT","DELETE")
.SetPreflightMaxAge(new TimeSpan(1728000))
.AllowAnyOrigin()
.AllowCredentials()
);
});
CorsAllowAll可以从IConfigurationStartup.cs 中访问设置值。根据它的值,可以Configure在调用之前在方法中全局设置定义的策略之一app.UseMvc()。
//Read value from appsettings
var corsAllowAll = Configuration["AppSettings:CorsAllowAll"] ?? "false";
app.UseCors(corsAllowAll == "true"? "CorsAllowAll" : "CorsAllowSpecific");
- 2 回答
- 0 关注
- 440 浏览
添加回答
举报