Oauth 2.0 如何使用Google Oauth2获得生日

Oauth 2.0 如何使用Google Oauth2获得生日,oauth-2.0,google-api,Oauth 2.0,Google Api,我想用谷歌Oauth2来庆祝生日 但不确定我是否做对了 以下是我正在使用的代码: var googleAuthOptions = new GoogleOAuth2AuthenticationOptions(); googleAuthOptions.ClientId = googleId; googleAuthOptions.ClientSecret = googleSecret; googleAuthOptions.Scope.A

我想用谷歌Oauth2来庆祝生日 但不确定我是否做对了

以下是我正在使用的代码:

        var googleAuthOptions = new GoogleOAuth2AuthenticationOptions();
        googleAuthOptions.ClientId = googleId;
        googleAuthOptions.ClientSecret = googleSecret;

        googleAuthOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
        googleAuthOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
        googleAuthOptions.Scope.Add("https://www.googleapis.com/auth/plus.login");

        googleAuthOptions.Provider = new GoogleOAuth2AuthenticationProvider
        {
            OnAuthenticated = (context) =>
            {
                context.Identity.AddClaim(new System.Security.Claims.Claim("GoogleAccessToken", context.AccessToken));

                var expiryDuration = context.ExpiresIn ?? new TimeSpan();
                context.Identity.AddClaim(new Claim("google:expires_in", DateTime.UtcNow.Add(expiryDuration).ToString(CultureInfo.InvariantCulture)));

                if (context.Email != null) context.Identity.AddClaim(new Claim("google:email", context.Email));
                if (context.Id != null) context.Identity.AddClaim(new Claim("google:id", context.Id));
                if (context.GivenName != null) context.Identity.AddClaim(new Claim("google:given_name", context.GivenName));
                if (context.FamilyName != null) context.Identity.AddClaim(new Claim("google:family_name", context.FamilyName));
                if (context.Name != null) context.Identity.AddClaim(new Claim("google:name", context.Name));
                if (context.Profile != null) context.Identity.AddClaim(new Claim("google:profile", context.Profile));

                if (context.User.GetValue("birthday") != null) context.Identity.AddClaim(new Claim("google:birthday", context.User.GetValue("birthday").ToString()));
                if (context.User.GetValue("locale") != null) context.Identity.AddClaim(new Claim("google:locale", context.User.GetValue("locale").ToString()));
                if (context.User.GetValue("gender") != null) context.Identity.AddClaim(new Claim("google:gender", context.User.GetValue("gender").ToString()));
                if (context.User.GetValue("picture") != null) context.Identity.AddClaim(new Claim("google:picture", context.User.GetValue("picture").ToString()));

                // Add all other available claims
                foreach (var claim in context.User)
                {
                    var claimType = string.Format("google:{0}", claim.Key);
                    var claimValue = claim.Value.ToString();
                    if (!context.Identity.HasClaim(claimType, claimValue))
                        context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Google"));
                }

                return Task.FromResult(0);
            }
        };
        app.UseGoogleAuthentication(googleAuthOptions);
我很想看到一个代码的例子,它可以确保你的生日


提前感谢。

已解决,因此除了设置正确的范围()

确保您的Google Plus测试帐户档案允许“显示生日年”,并且生日对“公众”可用

如果这还不够,谷歌还会以yyyy/mm/dd格式发送生日信息。。。 所以我用这个来显示MM/dd/yyyy

DateTime出生日期;
DateTime.TryParse(vm.DateOfBirth,out-DateOfBirth);
if(DateAndTime.IsDateValid(vm.DateOfBirth))
{
//默认情况下,Google返回yyyy/mm/dd
vm.DateOfBirth=DateOfBirth.ToString(@“MM/dd/yyyy”);
}
else vm.DateOfBirth=string.Empty