Nir*_*Sns 4 c# azure-active-directory microsoft-graph-api
我有一个使用 Azure Active Directory 授权(使用 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");
Run Code Online (Sandbox Code Playgroud)
您正在使用 Azure Graph API 中的 URI 模式调用 Microsoft Graph。这些是具有不同调用模式的不同 API。
正确的 URI 应该是:
https://graph.microsoft.com/v1.0/users?$filter=startsWith(userPrincipalName, 'string')
Run Code Online (Sandbox Code Playgroud)
默认情况下,/users只返回一组有限的属性。从文档:
默认情况下,只返回一组有限的属性(businessPhones、displayName、givenName、id、jobTitle、mail、mobilePhone、officeLocation、preferredLanguage、surname、userPrincipalName)。
要返回备用属性集,您必须使用 OData
$select查询参数指定所需的用户属性集。例如,要返回displayName、givenName和postalCode,您可以使用将以下内容添加到您的查询中$select=displayName,givenName,postalCode注意:某些属性不能在用户集合中返回。仅在检索单个用户时支持以下属性:aboutMe、生日、hireDate、兴趣、mySite、pastProjects、preferredName、责任、学校、技能、邮箱设置
换句话说,如果您需要除默认属性之外的其他内容,则需要明确请求您想要的属性集。
基于以上内容,您的示例代码应如下所示:
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");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2640 次 |
| 最近记录: |