ASP.Net Core 2 authentication write cookie to localhost different port

ASP.Net Core 2 authentication write cookie to localhost different port

我有一个 .net Core 2 web api 服务于一个 vue.js ,该服务将从子域提供服务。

我目前有 vue.js 客户端站点接受来自 ASP.Net Core Identity web api 的身份验证 cookie。我刚刚将客户端应用程序从 .net 核心应用程序中迁移出来,以便从节点实例提供服务。

我在本地进行调试,两者都是 运行 在具有不同端口的本地主机上。

我想知道是否可以通过指定 domains/hosts 身份服务来让 .net 核心身份向 vue.js 站点传送一个身份验证 cookie。

最终,客户端和服务器应用程序将从相同的域但不同的子域提供服务。

将您的 vue http 客户端设置为使用凭据。例如对于 axios 你会覆盖默认值 "withCredentials":

axios.defaults.withCredentials = true;

我通过在我的 axios 实例上添加 withCredentials: true 来解决

const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API_URL,
  timeout: 60000,
  withCredentials: true, <=========== ADDING
})

http://localhost:8080到下面指定的原点

services.AddCors(options =>
{
    options.AddPolicy("CorsPolicy", builder =>
    {
        builder.WithOrigins("http://localhost:8080") <=========== ADDING
            .AllowAnyMethod().AllowAnyHeader().AllowCredentials();
    });
});