简体   繁体   English

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

[英]Azure AD - get Group AD Name via Graph Service or claims

I'm trying to get the names of Group Azure AD, Inside token after Azure Login (openId) I receive group IDs in json But I need Groups names.我正在尝试获取 Azure AD 组的名称,Azure 登录后的内部令牌 (openId) 我在 json 中收到组 ID 但我需要组名称。

Json after login:登录后的json:

Claims理赔

trying to use GraphService At Azure portal, create a secret key but when I put these key in AuthenticationHeaderValue my code brokes.尝试在 Azure 门户中使用 GraphService,创建一个密钥,但是当我将这些密钥放入 AuthenticationHeaderValue 时,我的代码就崩溃了。

Portal Azure Add Key门户 Azure 添加密钥

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

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

My var "token" is the same token which i receive after login, and which have claims inside.我的 var "token" 与我在登录后收到的令牌相同,并且内部有声明。

At inner exception i receive:在内部异常我收到:

"Access token validation failure. Invalid audience" “访问令牌验证失败。受众无效”

Exception例外

What are the correct parameter i should to put in authentication?我应该在身份验证中输入的正确参数是什么?

AFter these invocation How i receive the name of GROUP?在这些调用之后,我如何收到 GROUP 的名称? Any suggestions to do that?有什么建议吗? I don't need role applications Names because i need group AD Names我不需要角色应用程序名称,因为我需要组 AD 名称

I think to try with the next line, but i don't know if inside these object I receive names of groups.我想尝试下一行,但我不知道在这些对象中我是否收到组名。

In these line, i expect to receive name of groups of these user, who correspon to these login token Groups在这些行中,我希望收到这些用户组的名称,这些用户对应于这些登录令牌

or with these line:或使用这些行:

grapServiceClient.Me grapServiceClient.Me

According to my research, if we configure Groups claim for the Azure AD application, it will just return the ObjectID of groups which the user used to login contains in the group claim value and on-premises group attributes.根据我的研究,如果我们为 Azure AD 应用程序配置Groups声明,它只会返回用户用于登录的组的 ObjectID,包含在组声明值和本地组属性中。 It cannot return group name.它不能返回组名。 For more details, please refer to the document .更多详细信息,请参阅文档 在此处输入图像描述

So if we want to get the groups name, we can use Microsoft Graph API to get it.所以如果我们想要获取组名,我们可以使用Microsoft Graph API来获取它。 But the api will return directory object and group object.但 api 将返回目录对象和组对象。 So we need to do some process.所以我们需要做一些处理。 For example 1. Register Azure AD application例如 1. 注册 Azure AD 应用程序

  1. Configure API permissions配置 API 权限在此处输入图像描述 在此处输入图像描述

  2. Update web.config更新 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. Add the follwoing code to ```Startup.cs将以下代码添加到```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. Call the graph api调用图形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;


                }


            }

For more details about how to develop the application, please refer to the document .有关如何开发应用程序的更多详细信息,请参阅文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM