2014-09-22 4 views
9

Ho visto molti post per l'autenticazione di facebook, sia quelli vecchi che non funzionanti correttamente come dovrebbe essere.ASP.NET MVC 5.1 C# OWIN autenticazione o login di facebook chiedi compleanno, Mi piace, profilo pubblico, numero di telefono

Comunque finalmente ho realizzato qualcosa nel mio progetto che funziona ma non completamente. Qui è il codice in cui chiedo

 var facebookAuthenticationOptions = new FacebookAuthenticationOptions() 
     { 
      AppId = "...ID", 
      AppSecret = "...AppSecret", 
      AuthenticationType = "Facebook", 
      SignInAsAuthenticationType = "ExternalCookie", 
      //Provider = new FacebookAuthenticationProvider 
      //{ 
      // OnAuthenticated = async ctx => 
      // { 
      //  if (ctx.User["birthday"] != null) 
      //  { 
      //   ctx.Identity.AddClaim(new Claim(ClaimTypes.DateOfBirth,ctx.User["birthday"].ToString())); 
      //  } 
      // } 
      //} 
     }; 
     facebookAuthenticationOptions.Scope.Add("user_birthday"); 
     //facebookAuthenticationOptions.Scope.Add("first_name"); 
     //facebookAuthenticationOptions.Scope.Add("last_name"); 
     //page goes blank if ask for first_name,last_name , if comment out works 
     //but doesn give date of birth or likes or other things. 
     facebookAuthenticationOptions.Scope.Add("publish_stream"); 
     facebookAuthenticationOptions.Scope.Add("user_likes"); 
     facebookAuthenticationOptions.Scope.Add("friends_likes"); 
     facebookAuthenticationOptions.Scope.Add("read_stream"); 
     facebookAuthenticationOptions.Scope.Add("email"); 

     app.UseFacebookAuthentication(facebookAuthenticationOptions); 

What I get from the return result.

Qualcuno può aiutarmi perché io non sto ottenendo friends_like, compleanno, user_likes e così via.

Inoltre, come ottenere i valori di stringa (es. "User_birthday", "first_name") per il recupero delle informazioni, come come faccio a saperlo che "user_birthday" restituisce la data del compleanno (l'ho preso dalla ricerca), è esiste una lista che abbia questo nome di stringa (es. "user_birthday", "first_name")?

Se utilizzo l'autenticazione google è possibile ottenere il numero di telefono o il nome, il cognome?

risposta

10

risolto, Riferimenti c'è: https://developers.facebook.com/docs/facebook-login/permissions/v2.1#reference

  var facebookAuthenticationOptions = new FacebookAuthenticationOptions() { 
      AppId = ".....", 
      AppSecret = ".....", 
      AuthenticationType = "Facebook", 
      SignInAsAuthenticationType = "ExternalCookie", 
      Provider = new FacebookAuthenticationProvider { 
       OnAuthenticated = async ctx => { 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.DateOfBirth, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.Country, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.Gender, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.MobilePhone, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.OtherPhone, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.HomePhone, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.StateOrProvince, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.Email, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.Country, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.Actor, ctx.User["birthday"].ToString())); 
        ctx.Identity.AddClaim(new Claim(ClaimTypes.DateOfBirth, ctx.User["birthday"].ToString())); 
       } 
      } 
     }; 
     facebookAuthenticationOptions.Scope.Add("user_birthday"); 
     //facebookAuthenticationOptions.Scope.Add("first_name"); 
     //facebookAuthenticationOptions.Scope.Add("last_name"); 
     facebookAuthenticationOptions.Scope.Add("publish_stream"); 
     facebookAuthenticationOptions.Scope.Add("user_likes"); 
     facebookAuthenticationOptions.Scope.Add("friends_likes"); 
     facebookAuthenticationOptions.Scope.Add("user_about_me"); 
     facebookAuthenticationOptions.Scope.Add("user_friends"); 
     facebookAuthenticationOptions.Scope.Add("user_actions.news"); 
     facebookAuthenticationOptions.Scope.Add("user_actions.video"); 
     facebookAuthenticationOptions.Scope.Add("user_education_history"); 
     facebookAuthenticationOptions.Scope.Add("manage_pages"); 
     facebookAuthenticationOptions.Scope.Add("user_interests"); 
     facebookAuthenticationOptions.Scope.Add("user_location"); 
     facebookAuthenticationOptions.Scope.Add("user_photos"); 
     facebookAuthenticationOptions.Scope.Add("user_relationships"); 
     facebookAuthenticationOptions.Scope.Add("user_relationship_details"); 
     facebookAuthenticationOptions.Scope.Add("user_status"); 
     facebookAuthenticationOptions.Scope.Add("user_tagged_places"); 
     facebookAuthenticationOptions.Scope.Add("user_videos"); 
     facebookAuthenticationOptions.Scope.Add("user_website"); 
     facebookAuthenticationOptions.Scope.Add("read_friendlists"); 
     facebookAuthenticationOptions.Scope.Add("read_stream"); 
     facebookAuthenticationOptions.Scope.Add("email"); 
     app.UseFacebookAuthentication(facebookAuthenticationOptions); 

CTX ottenuto tutte le informazioni .. resta che passare attraverso di essa.

Ancora non si dispone di informazioni su come ottenere google prima, cognome e numero di telefono + data di nascita. O come accedere ai contatti utente dall'account Gmail.

+0

Grazie. Ho sudato tutto questo giorno. – bowerm

+0

Grazie mille ~! per me funziona! Solo un problema è, gli oggetti di reclamo creati non vengono salvati nel mio database ..... che altro dovrei fare per farlo funzionare? – Franva

+1

@Franva devi gestirlo dal lato controller, devi accedere a queste proprietà e memorizzarle in db. –

7

Si può solo scorrere context.User per ottenere tutti i crediti

var facebookAuthOptions = new FacebookAuthenticationOptions(); 
     facebookAuthOptions.AppId = "..."; //Enter your AppId 
     facebookAuthOptions.AppSecret = "..."; //Enter your AppSecret 
facebookAuthOptions.Provider = new FacebookAuthenticationProvider() 
    { 
     OnAuthenticated = async context => 
      { 
       context.Identity.AddClaim(new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken)); 
       foreach (var claim in context.User) 
       { 
        var claimType = string.Format("urn:facebook:{0}", claim.Key); 
        string claimValue = claim.Value.ToString(); 
        if (!context.Identity.HasClaim(claimType, claimValue)) 
         context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Facebook")); 
       } 
      } 
    }; 

Lo stesso vale per Google

var googleAuthOptions = new GoogleOAuth2AuthenticationOptions(); 
     googleAuthOptions.ClientId = "..."; //Enter your ClientId 
     googleAuthOptions.ClientSecret = "..."; //Enter your ClientSecret 
googleAuthOptions.Provider = new GoogleOAuth2AuthenticationProvider() 
    { 
     OnAuthenticated = async context => 
     { 
      context.Identity.AddClaim(new System.Security.Claims.Claim("GoogleAccessToken", context.AccessToken)); 
      foreach (var claim in context.User) 
      { 
       var claimType = string.Format("urn:google:{0}", claim.Key); 
       string claimValue = claim.Value.ToString(); 
       if (!context.Identity.HasClaim(claimType, claimValue)) 
        context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Google")); 
      } 
     } 
    }; 

E credo che le impostazioni di privacy per l'utente e il vostro controllo autorizzazioni di applicazione che sostiene Puoi prendere.

Problemi correlati