C# 在MVC中从Google OAuth API检索出生日期
我添加了以下作用域以从用户访问数据C# 在MVC中从Google OAuth API检索出生日期,c#,asp.net-mvc,oauth,google-oauth,owin,C#,Asp.net Mvc,Oauth,Google Oauth,Owin,我添加了以下作用域以从用户访问数据 #region Google var googleAuthenticationOptions = new GoogleOAuth2AuthenticationOptions() { ClientId = ConfigurationManager.AppSettings["Google_AppID"], ClientSecret = ConfigurationManager.AppSettings
#region Google
var googleAuthenticationOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = ConfigurationManager.AppSettings["Google_AppID"],
ClientSecret = ConfigurationManager.AppSettings["Google_AppSecret"]
};
googleAuthenticationOptions.Scope.Add("profile");
googleAuthenticationOptions.Scope.Add("email");
googleAuthenticationOptions.Provider = new GoogleOAuth2AuthenticationProvider()
{
OnAuthenticated = async context =>
{
//context.Identity.AddClaim(new Claim("birthday", context.User.GetValue("birthday").ToString()));
string claimType;
bool bAddClaim = false;
foreach (var claim in context.User)
{
claimType = string.Empty;
bAddClaim = false;
switch (claim.Key)
{
case "given_name":
claimType = "FirstName";
bAddClaim = true;
break;
case "family_name":
claimType = "LastName";
bAddClaim = true;
break;
case "gender":
claimType = "gender";
bAddClaim = true;
break;
}
if (bAddClaim)
{
string claimValue = claim.Value.ToString();
if (!context.Identity.HasClaim(claimType, claimValue))
context.Identity.AddClaim(new System.Security.Claims.Claim(claimType, claimValue, "XmlSchemaString", "Google"));
}
}
}
};
在context.user中调试时,在每一个循环中放置断点
我得到以下信息
{
"sub": "101111724115925537597",
"name": "Alok Nath",
"given_name": "Alok",
"family_name": "Nath",
"profile": "https://plus.google.com/101111724115925537597",
"picture":"https://lh3.googleusercontent.com/XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/
photo.jpg",
"email": "aloknathbabuji786@gmail.com",
"email_verified": true,
"gender": "male",
"locale": "en"
}
{
“sub”:“101111724115925537597”,
“姓名”:“Alok Nath”,
“给定名称”:“Alok”,
“姓”:“纳特”,
“配置文件”:https://plus.google.com/101111724115925537597",
“图片”:https://lh3.googleusercontent.com/XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/
照片.jpg“,
“电子邮件”:aloknathbabuji786@gmail.com",
“电子邮件已验证”:正确,
“性别”:“男性”,
“区域设置”:“en”
}
因此,即使添加了“profile”范围,我也不会将生日作为返回值的一部分。我已确保档案的生日可见性设置为公共,即所有人都可以看到。要从谷歌检索生日,我需要做哪些特定的范围或特定的设置
或者是否有任何方法可以解决此问题请发布解决方案我认为您需要将范围设置为,以便获取此用户建议的出生日期: 更多信息也可在此处找到:
您是否尝试将nuget软件包Google.API.OAuth2.v2客户端库添加到您的项目中?这还为您的作用域提供了一个枚举,因此您可以不使用字符串,只说:
scope={Oauth2Service.scope.UserinfoProfile,Oauth2Service.scope.UserinfoEmail}