2017-09-02 2 views
0

J'essaie de configurer SSO sur mes applications ASP.NET sur le même domaine en utilisant Auth0 & OWIN. J'ai utilisé le tutoriel suivant pour setup my Owin Context.Auth0 ASP.Net.Owin SSO Valider le cookie interdomaine

J'ai configuré le cookie Auth0 avec un nom de domaine avec les & CookieAuthenticationOptions dans startup.cs:

string auth0Domain = ConfigurationManager.AppSettings["auth0:Domain"]; 
string auth0ClientId = ConfigurationManager.AppSettings["auth0:ClientId"]; 
string auth0ClientSecret = ConfigurationManager.AppSettings["auth0:ClientSecret"]; 

// Enable Kentor Cookie Saver middleware 
app.UseKentorOwinCookieSaver(); 
// Set Cookies as default authentication type 
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 
app.UseCookieAuthentication(new CookieAuthenticationOptions 
{ 
    //Add Cross domain 
    CookieName = "sso.example.com", 
    CookieDomain = ".example.com", 
    AuthenticationType = CookieAuthenticationDefaults.AuthenticationType, 
    LoginPath = new PathString("/Account/Login") 
}); 

Mon Auth0 configuration dans Startup.cs:

var options = new Auth0AuthenticationOptions() 
     { 
      Domain = auth0Domain, 
      ClientId = auth0ClientId, 
      ClientSecret = auth0ClientSecret, 
      Provider = new Auth0AuthenticationProvider 
      { 

       OnAuthenticated = context => 
       { 
        // Get the user's country 
        JToken countryObject = context.User["https://example.com/geoip"]; 
        if (countryObject != null) 
        { 
         string countryCode = countryObject["country_code"].ToObject<string>(); 
         string Lat = countryObject["latitude"].ToObject<string>(); 
         string Long = countryObject["longitude"].ToObject<string>(); 
         string City = countryObject["city_name"].ToObject<string>(); 
         string Country = countryObject["country_name"].ToObject<string>(); 

         context.Identity.AddClaim(new Claim("country_code", countryCode, ClaimValueTypes.String, context.Connection)); 
         context.Identity.AddClaim(new Claim("country_name", Country, ClaimValueTypes.String, context.Connection)); 
         context.Identity.AddClaim(new Claim("city_name", City, ClaimValueTypes.String, context.Connection)); 
         context.Identity.AddClaim(new Claim("longitude", Long, ClaimValueTypes.String, context.Connection)); 
         context.Identity.AddClaim(new Claim("latitude", Lat, ClaimValueTypes.String, context.Connection)); 
        } 
        JToken userMeta = context.User["https://example.com/user_metadata"]; 
        if (userMeta != null) 
        { 
         string companyName = userMeta["company"].ToObject<string>(); 
         context.Identity.AddClaim(new Claim("company", companyName, ClaimValueTypes.String, context.Connection)); 
         string fullName = userMeta["full_name"].ToObject<string>(); 
         context.Identity.AddClaim(new Claim("full_name", fullName, ClaimValueTypes.String, context.Connection)); 
        } 

        JToken rolesObject = context.User["https://example.com/app_metadata"]; 
        if (rolesObject != null) 
        { 
         string[] roles = rolesObject["roles"].ToObject<string[]>(); 
         foreach (var role in roles) 
         { 
          context.Identity.AddClaim(new Claim(ClaimTypes.Role, role, ClaimValueTypes.String, context.Connection)); 
         } 
        } 

        return Task.FromResult(0); 
       } 
      } 

     }; 
     options.Scope.Add("openid profile"); // Request a refresh_token 

Comment pourrais-je aller à propos de l'authentification du client sur l'application secondaire? Le cookie est disponible sur l'application secondaire mais je dois encore passer par le processus de connexion avec Auth0. Est-ce que je manque quelque chose? Ou y a-t-il un article que je peux lire sur l'implimentation?

Répondre

1

Je résolus en copiant les mêmes startup.cs sur les deux applications, et en ajoutant un machine key au fichier de configuration Web racine, dans le system.web tag.

Rien n'a changé depuis ma configuration initiale, je viens de changer les noms de domaine pour mon domaine.

+0

oui, les applications avec la même clé d'application partagent les mêmes cookies – Verthosa