ASP.NET MVC 5.1 C# OWIN facebook authentication or

2019-02-09 09:10发布

问题:

I have seen many posts for facebook authentication , either those are old or not working correctly as it should be.

However finally I have made something in my project that works but not fully. Here is code where I ask for

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

Can anyone please help me why I am not getting friends_like,birthday, user_likes and so on.

In addition, how to get those string values(ie. "user_birthday","first_name") for information retrieval, like how would I know that "user_birthday" returns date of birthday(I got it from searching) , is there any list which have this string names(ie. "user_birthday","first_name") ?

If I use google authentication is it possible to get phone number or first name,last name?

回答1:

Solved it, References there : 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 got all the information.. Just have to go through it.

Still don't have information on how to get google first,last name and phone number + date of birth. Or how to access user contacts from gmail account.



回答2:

You can just loop through context.User to get all claims

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

The same goes for 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"));
            }
        }
    };

And I think User's privacy settings and your application permissions control which claims you can get.