首页 文章

使用图形api在Azure Active目录组中搜索用户

提问于
浏览
4

我想在我的asp.net mvc 5应用程序中使用某些具有自动完整功能的人员选择器功能来搜索特定Azure AD组中的用户 . 这是一个演示“todo应用程序”,允许将todo分配给作为该组成员的用户 .

我尝试使用Graph API直接和Azure Graph Client库,但我似乎找不到实现我想要的方法 . 图api允许获取组的成员,但添加过滤器“startswith”失败,因为添加过滤器时api仅返回不包含例如DisplayName属性的目录对象...客户端库也没有多大帮助除了提供方法但有大量开销的批处理功能之外......我将不得不获得用户的过滤结果集,而不管组成员身份(使用api中的用户列表内容),该组的所有成员然后钓鱼使用Linq正确的结果集....可以正常开发/测试,但在 生产环境 与几百用户这将是疯了...

任何想法或建议将不胜感激 . 谢谢!

编辑

在我的代码下面,从客户端Javascript调用以搜索用户;

  • AccessGroupId是用于授权用户的Azure AD组 . 只有该组的成员才能访问我在自定义OWin中间件中处理的Web应用程序

  • 该方法旨在用于查找该组中的用户

代码工作正常如下,只是没有应用过滤,这是与输入参数pre(来自ui中的文本框)的intentaion . 我得到了访问组的所有成员 .

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);

1 回答

  • 1

    我将重点介绍两种可能的方法:

    • 使用自定义JS库对Graph API执行请求 . 您仍然需要关心accesstokens并查看ADAL.js

    一个示例应用程序(截至本文撰写时尚未最终确定),可在以下位置获取:AzureADSamples 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);
    

    }

    更详细的样本可在以下网址获得:AzureADSamples WebApp-GraphAPI-DotNet

相关问题