Xav*_*erk 5 c# search graph active-directory azure
我希望在我的 asp.net mvc 5 应用程序中具有某种具有自动完成功能的人员选择器功能,以搜索特定 Azure AD 组中的用户。这是一个演示“待办事项应用程序”,允许将待办事项分配给属于组成员的用户。
我直接尝试了 Graph API 和 Azure Graph Client 库,但似乎没有找到实现我想要的方法。图 api 允许获取组的成员,但添加过滤器“startswith”失败,因为在添加过滤器时,api 仅返回不包含例如 DisplayName 属性的目录对象......客户端库也没有太大帮助除了批处理功能提供了一种方法但有很多开销......然后我必须获得用户的过滤结果集,而不管组成员身份(使用 api 中的用户列表内容),组的所有成员,然后使用 Linq 找出正确的结果集......对于开发/测试来说可以正常工作,但在有几百个用户的生产中这将是疯狂的......
任何想法或建议将不胜感激。谢谢!
编辑
在我从客户端 Javascript 调用以搜索用户的代码下方;
代码工作正常,如下所示,只是没有应用过滤,这是输入参数 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);
}
Run Code Online (Sandbox Code Playgroud)
当我向同一个 api 调用添加过滤器而不是返回成员(用户、组和/或联系人)时,它会返回目录对象(没有 displayName),这些对象在上面的代码中并没有什么用,除非我愿意再次(批量)查询 api 以检索用户显示名称,但这对我来说似乎是很多开销。
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);
Run Code Online (Sandbox Code Playgroud)
我想强调两种可能的方法:
示例应用程序(截至撰写本文时尚未最终确定)可从以下位置获取: AzureADSamples WebApp-GroupClaims-DotNet
看看 AadPickerLibrary.js
它看起来像:
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);
Run Code Online (Sandbox Code Playgroud)
}
更详细的示例位于: AzureADSamples WebApp-GraphAPI-DotNet
归档时间: |
|
查看次数: |
8632 次 |
最近记录: |