【问题标题】:Azure AD - get Group AD Name via Graph Service or claimsAzure AD - 通过图形服务或声明获取组 AD 名称
【发布时间】:2019-11-28 21:24:14
【问题描述】:

我正在尝试获取组 Azure AD 的名称, Azure 登录 (openId) 后的内部令牌我收到 json 中的组 ID 但我需要组名称。

登录后的Json:

Claims

尝试使用 GraphService 在 Azure 门户中,创建一个密钥,但是当我将这些密钥放入 AuthenticationHeaderValue 时,我的代码会损坏。

Portal Azure Add Key

var graphServiceClientGr = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
                requestMessage
                    .Headers
                    .Authorization = new AuthenticationHeaderValue("Bearer", token);

                return Task.FromResult(0);
            }));

我的 var "token" 是我登录后收到的同一个令牌,里面有声明。

在我收到内部异常时:

“访问令牌验证失败。无效的受众”

Exception

我应该在身份验证中添加哪些正确参数?

在这些调用之后,我如何收到 GROUP 的名称?有什么建议吗? 我不需要角色应用程序名称,因为我需要组 AD 名称

我想尝试下一行,但我不知道在这些对象中我是否收到组的名称。

在这些行中,我希望收到这些用户组的名称,这些用户对应于这些登录令牌 Groups

或者用这些行:

grapServiceClient.Me

【问题讨论】:

标签: c# azure azure-active-directory microsoft-graph-api claims


【解决方案1】:

根据我的研究,如果我们为 Azure AD 应用程序配置 Groups 声明,它将只返回用户用于登录的组的 ObjectID,该组声明值和本地组属性中包含该组的对象ID。它不能返回组名。更多详情请参考document

所以如果我们想获取组名,我们可以使用Microsoft Graph API 来获取。但是 api 将返回目录对象和组对象。所以我们需要做一些处理。 例如 1.注册Azure AD应用

  1. 配置 API 权限

  2. 更新 web.config

<appSettings>
    <add key="ida:AppID" value="YOUR APP ID" />
    <add key="ida:AppSecret" value="YOUR APP PASSWORD" />
    <add key="ida:RedirectUri" value="https://localhost:PORT/" />
    <add key="ida:AppScopes" value="User.Read Directory.ReadWrite.All Directory.AccessAsUser.All />
</appSettings>
  1. 将以下代码添加到 ```Startup.cs
private async Task OnAuthorizationCodeReceivedAsync(AuthorizationCodeReceivedNotification notification)
        {
            var idClient = ConfidentialClientApplicationBuilder.Create(appId)
                .WithRedirectUri(redirectUri)
                .WithClientSecret(appSecret)
                .Build();

            string message;
            string debug;

            try
            {
                string[] scopes = graphScopes.Split(' ');

                var result = await idClient.AcquireTokenByAuthorizationCode(
                    scopes, notification.Code).ExecuteAsync();

                message = "Access token retrieved.";
                debug = result.AccessToken;
            }
            catch (MsalException ex)
            {
                message = "AcquireTokenByAuthorizationCodeAsync threw an exception";
                debug = ex.Message;
            }

            notification.HandleResponse();
            notification.Response.Redirect($"/Home/Error?message={message}&debug={debug}");
        }
  1. 调用图形接口
            var graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(
                    async (requestMessage) =>
                    {
                        requestMessage.Headers.Authorization =
                            new AuthenticationHeaderValue("Bearer", accessToken);
                    }));
            var results = await graphClient.Me.MemberOf.Request().GetAsync();
            var lists = results.ToList();
            Group group;
            foreach (var a in lists) {

                if (a.GetType() == typeof(Group)) {

                    group = a as Group;
                    var groupId=group.Id;
                    var groupName=group.DisplayName;


                }


            }

有关如何开发应用程序的更多详细信息,请参阅document

【讨论】:

  • 嗨,吉姆,我了解您的解决方案,我的代码与您的代码相似,但我不知道如何获得“accessToken”变量。注意:我使用的是 netcore 3.0,aspnetboilerplate,前面有 angular8
猜你喜欢
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 2020-02-28
  • 1970-01-01
相关资源
最近更新 更多