首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Asp.Net核心google-signin oauth限制访问并获得g-套件角色

Asp.Net核心google-signin oauth限制访问并获得g-套件角色
EN

Stack Overflow用户
提问于 2017-03-24 10:45:29
回答 1查看 1.1K关注 0票数 4

我正在制作一个具有web视图的.NET核心应用程序,在那里我需要使用Google+登录来验证用户。我遵循这个( https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins )教程,现在可以登录我的google帐户了。到目前一切尚好。

如何将对应用程序的访问仅限于某个域中的用户?

如何检索在G套件中定义的经过身份验证的用户角色?

我尝试在Startup.cs中为身份验证选项添加作用域,但我不知道如何提取/接收它们中包含的信息。

代码语言:javascript
复制
        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"]
        });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-27 12:09:25

最终解决了我自己..。留下这个答案以防其他人有这个问题。

来自AccountController.ExternalLoginCallback()的代码片段

代码语言:javascript
复制
    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()):

代码语言:javascript
复制
    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;
            }
        }
    });
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42997458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档