asp-CORS跨域

使用 nginx 做反向代理,监听 80 端口,/ 转发给 vue 10002 端口,/v1 转发给 asp.net core 的 48326 端口

asp.net

//在Startup中的ConfigureServices里增加
services.AddSession();
services.AddCors(options =>
{
options.AddPolicy("cors", builder =>
{
// 注意 有 80 和 没 80 是不同的
builder.AllowAnyOrigin().WithOrigins("*", "http://localhost", "http://127.0.0.1", "http://localhost:80", "http://127.0.0.1:80")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();//指定处理cookie
});
});

services.AddControllers();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
//修改配置,防止页面切换后session ID改变(默认是改变的,Seesion会失效)
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false;//这里要改为false,默认是true,true的时候session无效
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// 解决 反向代理时的 session id 变化问题
services.AddDataProtection();

//在Configure里增加:
app.UseCors("cors");
app.UseSession();

vue

也可以在调试的时候使用 proxy ,但是生产环境用不了,所以最好调试环境和生产环境保持一致。

const instance = axios.create({
withCredentials: true
});