我有一个 C# MVC 项目。我试图通过从 Google 检索用户信息来帮助用户注册过程。我想访问名字、姓氏、电子邮件和手机号码。都是必填字段。我相信我需要使用 Google People API。Google+ API 一直运行良好,但没有手机号码。我不确定如何获取这些数据。目前在 startup.auth 我有:
var googleOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = ConfigurationManager.AppSettings["GoogleClientId"],
ClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"],
Provider = new GoogleOAuth2AuthenticationProvider
{
OnAuthenticated = context =>
{
context.Identity.AddClaim(new Claim("urn:google:accesstoken", context.AccessToken, ClaimValueTypes.String, "Google"));
context.Identity.AddClaim(new Claim(ClaimTypes.Email, context.Email));
context.Identity.AddClaim(new Claim(ClaimTypes.Uri, context.User["image"]["url"].ToString()));
return Task.FromResult(true);
}
}
};
googleOptions.Scope.Add("https://www.googleapis.com/auth/user.phonenumbers.read");
googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
googleOptions.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
app.UseGoogleAuthentication(googleOptions);
在我的控制器中,我有:
if (loginInfo.Login.LoginProvider == "Google")
{
if (loginInfo.Email == null)
loginInfo.Email = GetSchemasClaimValue(loginInfo, "emailaddress");
firstName = GetSchemasClaimValue(loginInfo, "givenname");
lastName = GetSchemasClaimValue(loginInfo, "surname");
mobilePhone = loginInfo.ExternalIdentity.Claims.FirstOrDefault(c => c.Type == ClaimTypes.MobilePhone);
//proPic = GetSchemasClaimValue(loginInfo, "uri");
}
除了手机之外的所有信息都可以根据需要访问和工作。我只是不确定如何检索这些数据。我希望它会在 loginInfo 中显示为声明,但这种情况下不存在声明。提示用户授予应用访问手机的权限,所以我有点困惑为什么没有索赔。是否需要在我的 startup.auth 中添加声明?那将如何运作?任何帮助,将不胜感激。