使用图形api在Azure Active directory组中搜索用户
本文关键字:directory 搜索 用户 Active Azure 图形 api | 更新日期: 2023-09-27 18:25:39
我希望在我的asp.net mvc 5应用程序中拥有某种具有自动完成功能的人员选择器功能,以搜索特定Azure AD组中的用户。这是一个演示"todo应用程序",允许将todo分配给小组成员。
我直接尝试了Graph API和Azure Graph客户端库,但似乎找不到实现我想要的东西的方法。图形api允许获取组的成员,但添加筛选器"startswith"失败,因为添加筛选器时,api只返回不包括DisplayName属性的目录对象。。。除了批处理功能之外,客户端库也没有多大帮助,批处理功能提供了一种方法,但开销很大。。。然后,我必须获得一个过滤后的用户结果集,而不考虑组成员身份(使用api中的用户列表),组的所有成员,然后使用Linq找出正确的结果集。。。。对于开发/测试来说会很好,但在有几百个用户的生产中,这将是疯狂的。。。
任何想法或建议都将不胜感激。谢谢
编辑
下面我的代码是从客户端调用Javascript来搜索用户的;
- AccessGroupId是用于授权用户的Azure AD组。只有此组的成员可以访问我在自定义中处理的web应用程序OWin中间件
- 该方法旨在用于查找该组中的用户
代码运行良好,如下所示,只是没有应用过滤,这是输入参数pre(来自ui中的文本框)的意图。我得到了访问组的所有成员。
public async Task<JsonResult> FindUser(string pre)
{
string AccessGroupId = ConfigurationManager.AppSettings["AccessGroupId"];
AuthenticationContext authCtx = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, "{0}/{1}", SecurityConfiguration.LoginUrl, SecurityConfiguration.Tenant));
ClientCredential credential = new ClientCredential(SecurityConfiguration.ClientId, SecurityConfiguration.AppKey);
AuthenticationResult assertionCredential = await authCtx.AcquireTokenAsync(SecurityConfiguration.GraphUrl, credential);
var accessToken = assertionCredential.AccessToken;
var graphUrl = string.Format("https://graph.windows.net/mytenant.onmicrosoft.com/groups/{0}/members?api-version=2013-11-08, AccessGroupId );
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, graphUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage response = await client.SendAsync(request);
String responseString = await response.Content.ReadAsStringAsync();
JObject jsonReponse = JObject.Parse(responseString);
var l = from r in jsonReponse["value"].Children()
select new
{
UserObjectId = r["objectId"].ToString(),
UserPrincipalName = r["userPrincipalName"].ToString(),
DisplayName = r["displayName"].ToString()
};
//users = Newtonsoft.Json.JsonConvert.DeserializeObject<List<User>>(responseString);
return Json(l, JsonRequestBehavior.AllowGet);
}
当我在同一个api调用中添加一个过滤器,而不是返回成员(用户、组和/或联系人)时,它会返回目录对象(没有displayName),这些对象在上面的代码中并没有真正使用,除非我会再次(批量)查询api以检索用户的displayName,但这对我来说似乎是一个很大的开销。
var graphUrl = string.Format("https://graph.windows.net/mytenant.onmicrosoft.com/groups/{0}/members?api-version=2013-11-08&$filter=startswith(displayName,'{1}')", AccessGroupId, pre);
我强调两种可能的方法:
- 使用自定义JS库执行对Graph API的请求。您仍然需要关心accesstoken并查看ADAL.js
示例应用程序(截至本文撰写之时尚未最终确定)可在以下位置获得:AzureADS示例WebApp GroupClaims DotNet
看看AadPickerLibrary.js
- 尝试使用ActiveDirectoryClient
它看起来像:
public async Task<JsonResult> FindUser(string pre) {
ActiveDirectoryClient client = AADHelper.GetActiveDirectoryClient();
IPagedCollection<IUser> pagedCollection = await client.Users.Where(u => u.UserPrincipalName.StartsWith(pre, StringComparison.CurrentCultureIgnoreCase)).ExecuteAsync();
if (pagedCollection != null)
{
do
{
List<IUser> usersList = pagedCollection.CurrentPage.ToList();
foreach (IUser user in usersList)
{
userList.Add((User)user);
}
pagedCollection = await pagedCollection.GetNextPageAsync();
} while (pagedCollection != null);
}
return Json(userList, JsonRequestBehavior.AllowGet);
}
更详细的样本可在以下网址获得:AzureADS示例WebApp GraphAPI DotNet