c# - 使用 Microsoft Graph API(不是 Azure AD API)查询 Azure Active Directory 中的自定义属性

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

我有一个使用 Azure 事件目录授权(使用 ADAL)的 Asp.Net 网站,它返回基本属性,例如用户的显示名称。我想要的是使用 Microsoft Graph API(不是 Azure AD API)获取自定义属性(例如 employeeID)

示例图形 API 查询:

string graphRequest = string.Format(CultureInfo.InvariantCulture,
    "{0}{1}/users?api-version={2}&$filter=startswith(userPrincipalName, '{3}')",
    graphApiEndpoint,
    tenant,
    graphApiVersion,
    "SearchText.Text");

最佳答案

你正在使用来自 Azure Graph API 的 URI 模式调用 Microsoft Graph。这些是具有不同调用模式的不同 API。

正确的 URI 应该是:

https://graph.microsoft.com/v1.0/users?$filter=startsWith(userPrincipalName, 'string')

默认情况下,/users 只返回一组有限的属性。来自documentation :

By default, only a limited set of properties are returned ( businessPhones, displayName, givenName, id, jobTitle, mail, mobilePhone, officeLocation, preferredLanguage, surname, userPrincipalName ).

To return an alternative property set, you must specify the desired set of user properties using the OData $select query parameter. For example, to return displayName, givenName, and postalCode, you would use the add the following to your query $select=displayName,givenName,postalCode

Note: Certain properties cannot be returned within a user collection. The following properties are only supported when retrieving an single user: aboutMe, birthday, hireDate, interests, mySite, pastProjects, preferredName, responsibilities, schools, skills, mailboxSettings

换句话说,如果您需要默认属性以外的东西,您需要明确请求您想要的属性集。

基于以上内容,您的示例代码应如下所示:

const string graphApiEndpoint = "https://graph.microsoft.com";
const string graphApiVersion = "v1.0";
const string userProperties = "id,userPrincipalName,displayName,jobTitle,mail,employeeId"
string graphRequest = string.Format(CultureInfo.InvariantCulture,
    "{0}/{1}/users?$select={2}&$filter=startswith(userPrincipalName, '{3}')",
    graphApiEndpoint,
    graphApiVersion,
    userProperties,
    "SearchText.Text");

关于c# - 使用 Microsoft Graph API(不是 Azure AD API)查询 Azure Active Directory 中的自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52551930/

相关文章:

c# - 为什么我的 backgroundWorker 不工作?

azure - 无法获取订阅详细信息

microsoft-graph-api - 微软图形 API : Is there any limit on payload size of a POST request

javascript - 使用 "RSACryptoServiceProvider"公钥加密 Javascript 中的数据 -> 超出最大异常

c# - Linq:连接中的 == 和 equals 有什么区别?

azure - 是否可以使用 key 保管库 key 创建 JWT token ?

azure - 使用 microsoft graph 按办公地点获取用户

azure - Office 365 API ErrorAccessDenied 发送电子邮件时访问被拒绝

azure - 无法成功验证来自 Microsoft Graph API 的访问 token

c# - Xamarin Studio 的 intellisense 对于 LINQ 查询表现不佳