【问题标题】:AAD user extension properties using Microsoft graph client sdk使用 Microsoft 图形客户端 sdk 的 AAD 用户扩展属性
【发布时间】:2020-07-23 19:18:28
【问题描述】:

我正在尝试使用 Microsoft GraphClient sdk 获取添加到 Azure 活动目录中的用户的扩展属性。

即,我需要使用 Graph 客户端的以下命令的结果。

使用 Microsoft.Graph,版本=3.4.0.0。

PS C:\WINDOWS\system32> Get-AzureADUser -ObjectId 50413382@wingtiptoys.com |select -ExpandProperty 
ExtensionProperty

Key                                                             Value
---                                                             -----
odata.metadata                                                  https://graph.windows.net/d29b7a9b- 
6edb-4720-99a8-3c5c6c3eeeb0/$metadata#directoryObjects/@Element
odata.type                                                      Microsoft.DirectoryServices.User
createdDateTime
employeeId                                                      50413382
onPremisesDistinguishedName
thumbnailPhoto@odata.mediaEditLink                              directoryObjects/8cc715a1-0698-4d1a- 
8f49-441a84b6dbc4/Microsoft.DirectoryServices.User/thumbnailPhoto
thumbnailPhoto@odata.mediaContentType                           image/Jpeg
userIdentities                                                  []
extension_10a03227b5f146ad8a0087cf0bafd627_division             
|30103611|50435526|50230396|10192257|86009851
extension_10a03227b5f146ad8a0087cf0bafd627_company              wingtiptoys Inc.
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute10 GF
extension_10a03227b5f146ad8a0087cf0bafd627_employeeID           50413382
extension_10a03227b5f146ad8a0087cf0bafd627_cn                   50413382
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute8  wingtiptoys Inc. Inc.
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute7  Chuck
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute6  US11
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute5  US1-Rochester, NY- Site
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute4  USC
extension_10a03227b5f146ad8a0087cf0bafd627_extensionAttribute2  Regular
extension_10a03227b5f146ad8a0087cf0bafd627_employeeType         ARR

感谢任何帮助。

【问题讨论】:

  • 嗨 Sumith,请参考我在下面提供的解决方案。如果它对您的问题有帮助,请accept它作为答案(单击我的答案旁边的复选标记,将其从灰色切换为已填充)。先谢谢了~
  • 完美。有效!!!。感谢@Hury Shen 的明确解释。

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


【解决方案1】:

对于这个问题,我们在做之前需要知道一件事。

powershell 命令在后端请求“Azure AD graph api”而不是“Microsoft graph api”,因为我们可以看到主机是https://graph.windows.net....。如果使用“Microsoft graph api”,应该是https://graph.microsoft.com...

您请求的扩展程序只能通过“Azure AD graph api”访问,但不能通过“Microsoft graph api”访问,尽管有一个类似extension 的属性在“Microsoft graph api(get user)”的响应中。所以我们需要使用 Azure AD 图形 SDK 而不是使用Microsoft.Graph

根据网上搜索,关于如何使用 Azure AD 图形 SDK 的信息很少。而最新版的sdk更新在10/17/2016,因为微软很久没有更新“Azure AD graph”了。我不清楚如何使用 Azure AD graph SDK,所以我建议你直接在代码中请求 Azure AD graph api。你可以参考我下面的解决方案:

1.您需要在您的AD中注册一个应用并为其添加权限(添加Azure AD图形权限但不添加Microsoft图形权限)。

2.之后,我们可以通过以下代码请求Azure AD graph api:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApp25
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //request for the access token
            HttpClient client = new HttpClient();
            var values = new Dictionary<string, string>
            {
                { "client_id", "<client_id>" },
                { "scope", "https://graph.windows.net/.default" },
                { "client_secret", "<client_secret>" },
                { "grant_type", "client_credentials" },
            };
            var content = new FormUrlEncodedContent(values);
            var response = await client.PostAsync("https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token", content);
            var responseString = await response.Content.ReadAsStringAsync();

            //parse the responseString and get the access_token in it
            dynamic json = JsonConvert.DeserializeObject(responseString);
            var token = json.access_token;

            //use the access token to request the azure ad graph api
            HttpClient client1 = new HttpClient();
            client1.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
            var response1 = await client1.GetAsync("https://graph.windows.net/<tenant_id>/users/hury@xxx.onmicrosoft.com?api-version=1.6");
            var responseString1 = await response1.Content.ReadAsStringAsync();

            Console.WriteLine(responseString1);
        }
    }
}

responseString1包含了用户的所有字段,你需要解析json得到你想要的扩展名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    相关资源
    最近更新 更多