1

J'ai 3 applications Web sur Azure.Asp.Net Core 2 avec authentification Google Auth & Cookie sur les sous-domaines

  • webapp1 est www.mydomain.com
  • webapp2 est admin.mydomain.com
  • Webapp3 est user.mydomain.com

Lorsque je me connecte sur WebApp1 , Je veux être connecté sur tous les autres sous-domaines. Je veux utiliser des fournisseurs sociaux pour authentifier mes utilisateurs, et utiliser l'identité asp.net pour l'autorisation.

Après avoir lu docs & SO question ici est ce que j'ai dans mon Startup.cs

public void ConfigureServices(IServiceCollection services) 
{ 
    /* 
    * Some code 
    */ 

    // Creating a blob storage account to share keys for all applications 
    var storageAccount = CloudStorageAccount.Parse(configuration.GetConnectionString("IdentityStr")); 
    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 
    CloudBlobContainer container = blobClient.GetContainerReference("identity"); 
    AsyncContext.Run(() => container.CreateIfNotExistsAsync()); 

    services.AddDataProtection() 
      .SetApplicationName("MYAPP") 
      .PersistKeysToAzureBlobStorage(container, "keys.xml"); 

    /* 
    * BEGIN DISGUSTING: I recreate the data protection provider here 
    * because I need the instance of it below for the Cookie options 
    */ 
    var serviceCollection = new ServiceCollection(); 
    serviceCollection.AddDataProtection() 
      .SetApplicationName("MYAPP") 
      .PersistKeysToAzureBlobStorage(container, "keys.xml"); 
    var service2 = serviceCollection.BuildServiceProvider(); 
    var dataProtector = service2.GetRequiredService<IDataProtectionProvider>(); 
    /* 
    * END DISGUSTING 
    */ 

    services.AddDbContext<AuthDbContext>(options => 
      options.UseSqlServer(connectionString)); 

    services.AddIdentity<AuthUser, AuthRole>() 
      .AddEntityFrameworkStores<AuthDbContext>() 
      .AddDefaultTokenProviders(); 

    services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) 
      .AddCookie(o => 
      { 
       o.LoginPath = "/account/login"; 
       o.LogoutPath = "/account/logout"; 
       o.Cookie.Domain = "mydomain.com"; 
       o.DataProtectionProvider = dataProtector; 
       o.TicketDataFormat = new TicketDataFormat(dataProtector.CreateProtector("Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware", "Cookies", "v2")); 
      }) 
      .AddGoogle(o => 
      { 
       o.ClientId = configuration["Authentication:Google:ClientId"]; 
       o.ClientSecret = configuration["Authentication:Google:ClientSecret"]; 
      }); 

    /* 
    * Some code 
    */ 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    /* 
    * Some code 
    */ 

    app.UseAuthentication(); 

    /* 
    * Some code 
    */ 
} 

Le cookie fonctionne bien sur le webapp1 mais le domaine est attaché non pas comme celui défini dans o.Cookie. domaine mais www.mydomain.om

Voici une vue des cookies de chrome cookies

Et le point de vue Fiddler: enter image description here J'ai probablement raté quelque chose ...

Répondre

1

Le cookie d'identité n'a pas de jeu de domaines. Vous n'avez pas besoin d'ajouter une seconde fois Cookie, parce que l'identité ajoute déjà, et vous devez configurer cette instance, et non pas la nouvelle que vous créez

Donc, essayez en utilisant ConfigureApplicationCookie

services.AddIdentity<AuthUser, AuthRole>() 
     .AddEntityFrameworkStores<AuthDbContext>() 
     .AddDefaultTokenProviders(); 

services.AddAuthentication() 
     .AddGoogle(o => 
     { 
      // Google options. 
     }); 

services.ConfigureApplicationCookie(options => 
{ 
    // Cookie settings 
});