如何使用Google Oauth2生日 [英] How to get birthday using Google Oauth2

查看:64
本文介绍了如何使用Google Oauth2生日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google 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 =新的GoogleOAuth2AuthenticationProvider{OnAuthenticated =(上下文)=>{context.Identity.AddClaim(new System.Security.Claims.Claim("GoogleAccessToken",context.AccessToken));var expiryDuration = context.ExpiresIn?新的TimeSpan();context.Identity.AddClaim(new Claim("google:expires_in",DateTime.UtcNow.Add(expiryDuration).ToString(CultureInfo.InvariantCulture)));;如果(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));如果(context.GivenName!= null)context.Identity.AddClaim(new Claim("google:given_name",context.GivenName));如果(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()));如果(context.User.GetValue("locale")!= null)context.Identity.AddClaim(new Claim("google:locale",context.User.GetValue("locale").ToString()));如果(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()));//添加所有其他可用的声明foreach(在context.User中的var声明){varclaimType = string.Format("google:{0}",Claim.Key);var ClaimValue = Claim.Value.ToString();如果(!context.Identity.HasClaim(claimType,ClaimValue))context.Identity.AddClaim(new System.Security.Claims.Claim(claimType,ClaimValue,"XmlSchemaString","Google"));}返回Task.FromResult(0);}};app.UseGoogleAuthentication(googleAuthOptions); 

我很想看看能确保生日的代码示例

谢谢.

解决方案

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

I'm trying to get the birthday using Google Oauth2 but not sure if I'm doing it right...

Here is the code I'm using:

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

I would love to see a sample of code that works for sure to get the birthday

Thanks in advance.

解决方案

SOLVED, so other than setting the right scope (https://www.googleapis.com/auth/plus.login)

Make sure that your Google Plus test account profile allow "show birthday year" and also that the birthday is available to the "public"

if that's not enough, google also sends the birthday in yyyy/mm/dd format... So I'm using this to display MM/dd/yyyy

DateTime dateDateOfBirth;
DateTime.TryParse(vm.DateOfBirth, out dateDateOfBirth);
if (DateAndTime.IsDateValid(vm.DateOfBirth))
{
// By default Google returns yyyy/mm/dd
vm.DateOfBirth = dateDateOfBirth.ToString(@"MM/dd/yyyy");
}
else vm.DateOfBirth = string.Empty;

这篇关于如何使用Google Oauth2生日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆