.net core跨域传递cookie

后端

1、Startup.cs 中设置允许传递凭据

            application.UseCors(builder =>
            {
                builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials();
            });

2、设置cookie,需要带上设置成 SameSiteMode.None

            var options = new CookieOptions
            {
                HttpOnly = true,
                Expires = cookieExpiresDate,
                SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None
            };
            _httpContextAccessor.HttpContext.Response.Cookies.Append(cookieName, userGuid.ToString(), options);

前端

在fetch中设置请求时附带cookie信息

fetch(url, {
    method: 'POST',
    credentials: 'include',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    body: JSON.stringify(postData)
}).then(function(response) {
    console.log(response);
});

猜你喜欢

转载自www.cnblogs.com/amuro/p/10179548.html