Azure AD-通过图形服务或声明获取组AD名称

问题描述 投票:0回答:1

我正在尝试获取Azure 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“令牌”与我登录后收到的令牌相同,并且里面有声明。

由于内部异常,我收到:

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

Exception

我应该在身份验证中输入正确的参数是什么?

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

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

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

或以下行:

grapServiceClient.Me

c# azure azure-active-directory microsoft-graph claims
1个回答
0
投票

根据我的研究,如果我们为Azure AD应用程序配置Groups声明,它将仅返回用户用来登录的组的ObjectID,该用户ID包含在组声明值和本地组属性中。它不能返回组名。有关更多详细信息,请参见documententer image description here

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

  1. 配置API权限enter image description hereenter image description here

  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. 调用图形api
            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

© www.soinside.com 2019 - 2024. All rights reserved.