2014-09-22 2 views
9

Ich habe viele Beiträge für Facebook-Authentifizierung gesehen, entweder die sind alt oder nicht richtig funktioniert, wie es sein sollte.ASP.NET MVC 5.1 C# OWIN Facebook-Authentifizierung oder Login Fragen Sie nach Geburtstag, Likes, öffentliches Profil, Telefonnummer

Aber schließlich habe ich etwas in meinem Projekt gemacht, das funktioniert aber nicht vollständig. Hier ist der Code, wo ich fragen Sie nach

 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.

Kann jemand bitte helfen Sie mir, warum ich nicht friends_like, Geburtstag bekommen, user_likes und so weiter.

Darüber hinaus, wie diese String-Werte (dh. "User_birthday", "first_name") für den Informationsabruf, wie würde ich wissen, dass "user_birthday" Datum der Geburtstags (ich habe es von der Suche) zurückgibt, ist Gibt es irgendeine Liste, die diese String-Namen haben (zB. "user_birthday", "first_name")?

Wenn ich Google Authentifizierung verwende, ist es möglich, Telefonnummer oder Vorname, Nachname zu bekommen?

Antwort

10

es wurde gelöst, Referenzen dort: 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 bekam alle Informationen .. Nur durch sie gehen.

Noch haben keine Informationen darüber, wie Google zuerst, Nachname und Telefonnummer + Geburtsdatum bekommen. Oder wie Sie auf Benutzerkontakte von Google Mail-Konto zugreifen.

+0

Danke. Ich habe den ganzen Tag geschwitzt. – bowerm

+0

Vielen Dank ~! Für mich geht das! Das einzige Problem ist, dass die erstellten Claim-Objekte nicht in meiner Datenbank gespeichert werden. Was soll ich noch tun, damit es funktioniert? – Franva

+1

@Franva müssen Sie es von der Controller-Seite behandeln, müssen Sie auf diese Eigenschaften zugreifen und diese in db speichern. –

7

Sie können nur eine Schleife durch context.User alle Ansprüche zu bekommen

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")); 
       } 
      } 
    }; 

Das gleiche gilt für Google-Einstellungen und Ihre Anwendungsberechtigungen Steuer

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")); 
      } 
     } 
    }; 

Und ich denke, der Privatsphäre geht, die behauptet, du kannst bekommen.

Verwandte Themen