【发布时间】:2020-07-17 06:07:57
【问题描述】:
我们正在 .NET Core 中尝试外部登录 (Identityserver4),并且我们使用了外部提供商,即 Google。我们能够在登录期间检索谷歌用户的名字、姓氏、电子邮件等数据。但我们无法检索用户的电话号码和其他附加声明。
Startup.Auth.cs:
services.AddAuthentication().AddGoogle("Google", options =>
{
options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ClientId = "XXX";
options.ClientSecret = "XXX";
options.UserInformationEndpoint = "https://www.googleapis.com/oauth2/v2/userinfo";
options.ClaimActions.MapJsonKey(ClaimTypes.OtherPhone, "otherphone");
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.ClaimActions.MapJsonKey("urn:google:locale", "locale", "string");
options.ClaimActions.MapJsonKey("urn:google:MobilePhone", "mobilephone", "string");
options.ClaimActions.MapJsonKey("urn:google:gender", "gender", "string");
options.ClaimActions.MapJsonKey("urn:google:birthday", "birthday", "date");
options.ClaimActions.MapJsonKey("urn:google:accesstoken", "AccessToken", "string");
options.ClaimActions.MapJsonKey(ClaimTypes.Gender, "gender");
options.SaveTokens = true;
});
我们已添加options.ClaimActions.MapJsonKey("urn:google:MobilePhone", "mobilephone", "string"); 这一行,但我们没有收到手机号码。同一行代码适用于图片,但手机号码无效。
【问题讨论】:
-
如果您想获取用户电话号码,您必须获得用户的授权:请参阅以下信息页面:developers.google.com/admin-sdk/directory/v1/guides/authorizing
-
谢谢!希望这会有所帮助
标签: asp.net-core .net-core oauth-2.0 identityserver4