【问题标题】:Querying Custom attributes in Azure Active Directory using Microsoft Graph API (NOT Azure AD API)使用 Microsoft Graph API(不是 Azure AD API)查询 Azure Active Directory 中的自定义属性
【发布时间】:2018-09-28 09:07:31
【问题描述】:

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

Graph API 查询示例:

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

【问题讨论】:

  • 自定义属性是什么意思?如果要获取不同的属性集,可以使用 OData $select query parameter.$select=employeeID。还是您的意思是 Azure B2c AD 用户自定义属性?
  • 是的,它是 B2C Active Directory 自定义属性。

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


【解决方案1】:

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

正确的 URI 应该是:

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

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

默认情况下,只返回一组有限的属性(businessPhones、displayName、givenName、id、jobTitle、mail、mobilePhone、officeLocation、preferredLanguage、surname、userPrincipalName)。

要返回备用属性集,您必须使用 OData $select 查询参数指定所需的用户属性集。例如,要返回 displayNamegivenNamepostalCode,您可以在查询中添加以下内容 $select=displayName,givenName,postalCode

注意:某些属性不能在用户集合中返回。仅在检索单个用户时支持以下属性:aboutMe、birthday、hireDate、interests、mySite、pastProjects、preferredName、responsibility、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");

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多