Asp.Net Core google-signin oauth 限制访问并获得 g-suite 角色

标签 asp.net oauth-2.0 google-oauth google-signin

我正在制作一个带有 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/

相关文章:

asp.net - 如何动态分配数据源给listview

带有空数组的 ASP.NET MVC Int Array 参数默认为 {0}

ios - OAuth 集成错误

authentication - oauth2orize 与 API?

ios8 - GIDSignIn 返回 "Keychain error"

Asp.net MVP - 创建动态控件,在哪里?

javascript - 用于动态创建控件的日期选择器

java - 如何使用 java 和 oauth 2.0 在 Azure AD 中注册应用程序来阅读我的 Outlook 邮件

android - 适用于移动应用程序的 Google OAuth2 工作流程

google-api - Google 登录网站 : read user's phone number