@@ -89,13 +89,12 @@ namespace YhCore.WebApi
|
||||
// 对象映射:对应 Java ModelMapper
|
||||
services.AddAutoMapper(typeof(Startup));
|
||||
|
||||
// 跨域策略:来源白名单(appsettings.json Cors:Origins)+ 允许凭据。
|
||||
// 响应回 Access-Control-Allow-Origin: <具体来源> 且 Access-Control-Allow-Credentials: true,
|
||||
// 白名单内前端可用 fetch/XHR credentials:'include' 跨域携带 CAS 登录 Cookie。
|
||||
// 注意:Cors 规范禁止 * 与凭据同用,故不能用 AllowAnyOrigin。
|
||||
var corsOrigins = Configuration.GetSection("Cors:Origins").Get<string[]>() ?? Array.Empty<string>();
|
||||
// 跨域策略:允许任意来源 + 允许凭据(等价 Java @CrossOrigin(originPatterns={"*"}, allowCredentials="true"))。
|
||||
// SetIsOriginAllowed(_ => true) 会回显任意请求 Origin(而非下发 *),
|
||||
// 并附 Access-Control-Allow-Credentials: true,使任意来源的前端都能以 credentials:'include' 跨域携带 CAS 登录 Cookie。
|
||||
// 注意:Cors 规范禁止 * 与凭据同用,故不能直接用 AllowAnyOrigin。
|
||||
services.AddCors(options => options.AddPolicy("default", builder => builder
|
||||
.WithOrigins(corsOrigins)
|
||||
.SetIsOriginAllowed(_ => true)
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader()
|
||||
.AllowCredentials()));
|
||||
|
||||
Reference in New Issue
Block a user