.net core 实现跨域

1、设置允许所有来源跨域

在StartUp类的ConfigureServices方法中添加如下代码

// 配置跨域处理,允许所有来源
services.AddCors(options =>
options.AddPolicy("AllCors",
p => p.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials()));

2、修改Configure方法

// 允许所有跨域,AllCors是在ConfigureServices方法中配置的跨域策略名称
app.UseCors("AllCors");

3、跨域中cookie使用

前端Ajax添加以下参数:

xhrFields: {
withCredentials: true
},
crossDomain: true,

 

posted @ 2021-04-01 15:48  有只煤球  阅读(239)  评论(0)    收藏  举报