【发布时间】:2019-12-11 14:20:06
【问题描述】:
应用:.Net Core 3 with Identity。
当应用通过 Google 进行身份验证时,它会获取照片、区域设置、用户名、用户名... 但是我不能过生日。我在互联网上搜索了两天,但没有得到解决方案。
Startup.cs
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;
};
});
帐户/ExternalLoginModel.cs
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);
}
}
在https://developers.google.com/people/api/rest/v1/people/get我过生日:
"birthdays": [
{
"metadata": {
"primary": true,
"source": {
"type": "PROFILE",
"id": "11"
}
},
"date": {
"year": 1930,
"month": 12,
"day": 26
}...
如何获得 .Net Core 生日声明?
【问题讨论】:
-
您是否尝试过向 google people api 发出请求并以这种方式取回?
-
@DaImTo 我在这个docs.microsoft.com/en-us/aspnet/core/security/authentication/… 工作过。而且我不知道我该如何尝试?
标签: asp.net-core google-oauth google-authentication