第一步:Startup添加如下代码
第二步:功能实现
/// <summary> /// 跨域配置 /// </summary> public class CorsOriginsConfig { /// <summary> /// 设置 /// </summary> public static void Set(IAppBuilder app) { //不允许跨域 if (PublicVars.CorsOrigins == "") { return; } //允许所有域 if (PublicVars.CorsOrigins == "*") { app.UseCors(CorsOptions.AllowAll);//信任所有站点 return; } //指定域 string[] arrOrigin = PublicVars.CorsOrigins.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); app.UseCors(new CorsOptions { PolicyProvider = new CorsPolicyProvider { PolicyResolver = context => { var policy = new CorsPolicy(); foreach (var item in arrOrigin) { policy.Origins.Add(item); } return Task.FromResult(policy); } } }); } }
标签:CorsOrigins,return,跨域,app,Asp,policy,new,net From: https://www.cnblogs.com/5tomorrow/p/17514203.html