我正在制作一个具有web视图的.NET核心应用程序,在那里我需要使用Google+登录来验证用户。我遵循这个( https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins )教程,现在可以登录我的google帐户了。到目前一切尚好。
如何将对应用程序的访问仅限于某个域中的用户?
如何检索在G套件中定义的经过身份验证的用户角色?
我尝试在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"]
});发布于 2017-03-27 12:09:25
最终解决了我自己..。留下这个答案以防其他人有这个问题。
来自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;
}
}
});https://stackoverflow.com/questions/42997458
复制相似问题