使用来自 Google auth (SPA) 的 IdentityServer 获取姓名和电子邮件声明
Get name and email claim using IdentityServer from Google auth (SPA)
我刚开始使用 Identity Server 进行 SPA 身份验证,但我开始关注这个示例:Authentication and authorization for SPAs 并且经过一些修改,我现在还添加了 Google 身份验证。但是,我无法将外部 Google 声明合并到我的应用程序声明中(例如:given_name
)。
我已经确认 Google 确实发回了适当的声明,但似乎没有映射这些声明,例如options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
。当我访问我的受保护端点之一时,我的声明不包括任何其他 google 声明。
我确实找到了一些额外的文档 Persist additional claims...,它告诉我在 OnPostConfirmationAsync (Account/ExternalLogin.cshtml.cs)
中添加声明,但由于这是一个 SPA,该页面不存在。还有另一种方法吗?我找不到很多不使用 Page / OnPostConfirmationAsync
.
的东西
谢谢
包括来自我的 Startup.cs
的相关细节,以防我在这里做错什么:
我尝试了一些与我发现的其他示例不同的变体,但是
services
.AddDefaultIdentity<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<AppRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer()
.AddApiAuthorization<AppUser, ApplicationDbContext>();
services
.AddAuthentication()
.AddIdentityServerJwt()
.AddGoogle(options =>
{
options.ClientId = Configuration["Auth:Google:ClientId"];
options.ClientSecret = Configuration["Auth:Google:ClientSecret"];
options.AuthorizationEndpoint += "?prompt=consent"; // Hack so we always get a refresh token, it only comes on the first authorization response
options.AccessType = "offline";
options.SaveTokens = true;
options.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
options.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
})
而我的 api 就是:
[Authorize()]
[Route("test")]
public IActionResult Test()
{
var all = User.Claims.Select(s => $"{s.Type}: {s.Value}");
return Ok(all);
}
我能够处理此问题的唯一方法是 scaffold the needed ExternalLogin page (like @d_f mentioned in the question's comments) and then continue following the Persist additional claims 步骤。我希望我可以设置一个声明集合以保存在我的启动文件中并且它可以工作但是身份服务器只使用它的内部 ExternalLogin
(Microsoft.AspNetCore.Identity.UI.V4.Pages.Account.Internal) 和那里的代码不处理添加外部声明。这目前有效,但我更喜欢一种不需要构建 ExternalLogin 页面的方法。
我试图在官方文档中寻求澄清,但我的问题已被关闭(相反,他们建议我在 Whosebug 上提问 - 哈哈)。然而,似乎有一些工作正在改进文档和流传声明,如果有兴趣你可以深入研究这个 GitHub 问题:https://github.com/dotnet/AspNetCore.Docs/issues/16488
我刚开始使用 Identity Server 进行 SPA 身份验证,但我开始关注这个示例:Authentication and authorization for SPAs 并且经过一些修改,我现在还添加了 Google 身份验证。但是,我无法将外部 Google 声明合并到我的应用程序声明中(例如:given_name
)。
我已经确认 Google 确实发回了适当的声明,但似乎没有映射这些声明,例如options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
。当我访问我的受保护端点之一时,我的声明不包括任何其他 google 声明。
我确实找到了一些额外的文档 Persist additional claims...,它告诉我在 OnPostConfirmationAsync (Account/ExternalLogin.cshtml.cs)
中添加声明,但由于这是一个 SPA,该页面不存在。还有另一种方法吗?我找不到很多不使用 Page / OnPostConfirmationAsync
.
谢谢
包括来自我的 Startup.cs
的相关细节,以防我在这里做错什么:
我尝试了一些与我发现的其他示例不同的变体,但是
services
.AddDefaultIdentity<AppUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<AppRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddIdentityServer()
.AddApiAuthorization<AppUser, ApplicationDbContext>();
services
.AddAuthentication()
.AddIdentityServerJwt()
.AddGoogle(options =>
{
options.ClientId = Configuration["Auth:Google:ClientId"];
options.ClientSecret = Configuration["Auth:Google:ClientSecret"];
options.AuthorizationEndpoint += "?prompt=consent"; // Hack so we always get a refresh token, it only comes on the first authorization response
options.AccessType = "offline";
options.SaveTokens = true;
options.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
options.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
options.ClaimActions.MapJsonKey(ClaimTypes.GivenName, "given_name");
})
而我的 api 就是:
[Authorize()]
[Route("test")]
public IActionResult Test()
{
var all = User.Claims.Select(s => $"{s.Type}: {s.Value}");
return Ok(all);
}
我能够处理此问题的唯一方法是 scaffold the needed ExternalLogin page (like @d_f mentioned in the question's comments) and then continue following the Persist additional claims 步骤。我希望我可以设置一个声明集合以保存在我的启动文件中并且它可以工作但是身份服务器只使用它的内部 ExternalLogin
(Microsoft.AspNetCore.Identity.UI.V4.Pages.Account.Internal) 和那里的代码不处理添加外部声明。这目前有效,但我更喜欢一种不需要构建 ExternalLogin 页面的方法。
我试图在官方文档中寻求澄清,但我的问题已被关闭(相反,他们建议我在 Whosebug 上提问 - 哈哈)。然而,似乎有一些工作正在改进文档和流传声明,如果有兴趣你可以深入研究这个 GitHub 问题:https://github.com/dotnet/AspNetCore.Docs/issues/16488