Asp.net core 为什么';谷歌提供商认证不存在生日声明?
应用程序:。具有标识的网络核心3 当应用程序通过谷歌认证时,它会得到照片、区域设置、用户名、用户名。。。 但是我不能过生日。我在网上搜索了两天,但没有找到解决方案 Startup.csAsp.net core 为什么';谷歌提供商认证不存在生日声明?,asp.net-core,google-oauth,google-authentication,Asp.net Core,Google Oauth,Google Authentication,应用程序:。具有标识的网络核心3 当应用程序通过谷歌认证时,它会得到照片、区域设置、用户名、用户名。。。 但是我不能过生日。我在网上搜索了两天,但没有找到解决方案 Startup.cs services.AddAuthentication() .AddGoogle(options => { IConfigurationSection googleA
services.AddAuthentication()
.AddGoogle(options =>
{
IConfigurationSection googleAuthNSection =
Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
// scope for birthday
options.Scope.Add("https://www.googleapis.com/auth/user.birthday.read");
options.AuthorizationEndpoint += "?prompt=consent";
options.AccessType = "offline";
options.ClaimActions.MapJsonKey("avatar", "picture", "url");
options.ClaimActions.MapJsonKey("locale", "locale", "string");
// all they don't work:
//options.ClaimActions.MapJsonKey("birthday", "birthday", ClaimValueTypes.String);
//options.ClaimActions.MapJsonKey(ClaimTypes.DateOfBirth, "birthdays");
options.ClaimActions.MapJsonKey(ClaimTypes.DateOfBirth, "birthdays", ClaimValueTypes.String);
options.SaveTokens = true;
options.Events.OnCreatingTicket = ctx =>
{
List<AuthenticationToken> tokens = ctx.Properties.GetTokens().ToList();
tokens.Add(new AuthenticationToken()
{
Name = "TicketCreated",
Value = DateTime.UtcNow.ToString()
});
ctx.Properties.StoreTokens(tokens);
return Task.CompletedTask;
};
});
我如何才能参加.Net核心生日申请?我也面临同样的问题。此外,我还尝试使用Google OpenId Connect REST API,即使我将“”范围添加到身份验证请求中,也不会返回所需的数据 根据我的搜索,“性别”和“生日”等字段不是标准的一部分,您需要访问提供商API才能获得此类信息 对我来说,我使用的是IdentityServer4,因此我在ProfileService中添加了blow代码,以获取所需的声明并将其添加到用户声明中:
//从访问令牌创建所需的凭据
//PeopleServiceNuget:Google.api.PeopleService.v1
var cred=GoogleCredential.FromAccessToken(“访问令牌”);
var peopleServiceService=新的peopleServiceService(
新的BaseClientService.Initializer(){HttpClientInitializer=cred});
//使用people/me访问当前已验证用户的数据
var personRequest=peopleServiceService.People.Get(“People/me”);
personRequest.personfeelds=“生日,性别”;
Person=peopleRequest.Execute();
//您现在可以访问
//人,性别,人,生日
在您的情况下,您可以在执行“OnCreatingTicket”时添加索赔:
//options.Events.OnCreatingTicket=ctx=>{-->
Person=peopleRequest.Execute();
var genderValue=person.Genders?.FirstOrDefault()?.FormattedValue;
var claimsIdentity=ctx.Principal.identies.First();
添加索赔(新索赔(“性别”,genderValue));
添加(新的AuthenticationToken()
{
Name=“TicketCreated”,
Value=DateTime.UtcNow.ToString()
});
人员API文档:
你有没有试过向google people api发出请求,然后通过这种方式将其取回?@DaImTo我就是这么做的。我不知道该怎么做?
public async Task<IActionResult> OnGetCallbackAsync(string returnUrl = null, string remoteError = null)
{
//....
var info = await _signInManager.GetExternalLoginInfoAsync();
// Sign in the user with this external login provider if the user already has a login.
var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
foreach (var i in info.Principal.Claims)
{
// get all claims from Google, except birthday.
Console.WriteLine(i.Type + " " + i.Value);
}
}
"birthdays": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "11"
}
},
"date": {
"year": 1930,
"month": 12,
"day": 26
}...