ASP.NET MVC 5.1 C# OWIN facebook authentication or login ask for birthday, likes, public profile, phone number

[亡魂溺海] 提交于 2019-11-30 09:02:26

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!