我正在制作一个带有 Web View 的 .NET Core 应用程序,我需要在其中使用 Google+ 登录对用户进行身份验证。
我遵循了这个( https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins )教程,现在可以登录我的谷歌帐户。到现在为止还挺好。
如何将我的应用程序的访问权限限制为仅特定域内的用户?
如何检索在 g-suite 中定义的经过身份验证的用户角色?
我曾尝试向 Startup.cs 中的身份验证选项添加范围,但我不知道如何提取/接收其中包含的信息。
app.UseGoogleAuthentication(new GoogleOptions()
{ Scope =
{ "https://www.googleapis.com/auth/admin.directory.domain.readonly",
"https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly"
},
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"]
});
最佳答案
好吧,最后我自己解决了......留下这个答案,以防其他人遇到这个问题。
来自 AccountController.ExternalLoginCallback() 的代码片段
var info = await _signInManager.GetExternalLoginInfoAsync();
foreach (var claim in info.Principal.Claims)
{
System.Diagnostics.Debug.WriteLine("Type: " + claim.Type + " Value: " + claim.Value);
}
这将打印声明列表,其中定义了角色和域 (hd)。
或者,您可以在中间件 (Startup.configure()) 中检索声明:
app.UseGoogleAuthentication(new GoogleOptions()
{
Scope =
{ "https://www.googleapis.com/auth/admin.directory.domain.readonly",
"https://www.googleapis.com/auth/admin.directory.rolemanagement.readonly"
},
ClientId = Configuration["Authentication:Google:ClientId"],
ClientSecret = Configuration["Authentication:Google:ClientSecret"],
Events = new OAuthEvents()
{
OnTicketReceived = e =>
{
var claims = e.Principal.Claims;
// Do something with claims
return Task.CompletedTask;
}
}
});
关于Asp.Net Core google-signin oauth 限制访问并获得 g-suite 角色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997458/