简体   繁体   中英

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

I have a .net Core 2 web api that services a vue.js that will be served from a subdomain.

I currently have the vue.js client site accepting auth cookies from the ASP.Net Core Identity web api. I have just migrated the client app out of the .net core app so that it is served from a node instance.

Locally I am debugging and both are running on localhost with different ports.

I'm wondering if it's possible to have the .net core identity deliver an auth cookie to the vue.js site by specifying the domains/hosts Identity should service.

Ultimately the client and server apps will be served from the same domain but different subdomains.

Set your vue http client to use credentials. For example for axios you'd override default value "withCredentials":

axios.defaults.withCredentials = true;

I solved by adding withCredentials: true on my axios instance

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

and http://localhost:8080 to the origin which is specified below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM