Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用图形api在Azure Active directory组中搜索用户_C#_Search_Azure_Graph_Active Directory - Fatal编程技术网

C# 使用图形api在Azure Active directory组中搜索用户

C# 使用图形api在Azure Active directory组中搜索用户,c#,search,azure,graph,active-directory,C#,Search,Azure,Graph,Active Directory,我希望在我的asp.net mvc 5应用程序中具有某种具有自动完成功能的人员选取功能,以搜索特定Azure广告组中的用户。这是一个演示“todo应用程序”,允许将todo分配给组中的用户 我尝试直接使用Graph API和Azure Graph客户端库,但似乎没有找到实现我想要的方法。graph api允许获取组成员,但添加筛选器“startswith”失败,因为添加筛选器时,api仅返回目录对象,其中不包括例如DisplayName属性。。。除了批处理功能之外,客户端库也没有什么帮助,它提供

我希望在我的asp.net mvc 5应用程序中具有某种具有自动完成功能的人员选取功能,以搜索特定Azure广告组中的用户。这是一个演示“todo应用程序”,允许将todo分配给组中的用户

我尝试直接使用Graph API和Azure Graph客户端库,但似乎没有找到实现我想要的方法。graph api允许获取组成员,但添加筛选器“startswith”失败,因为添加筛选器时,api仅返回目录对象,其中不包括例如DisplayName属性。。。除了批处理功能之外,客户端库也没有什么帮助,它提供了一种方法,但是有很多开销。。。然后,我必须得到一个过滤后的用户结果集,而不考虑组成员身份(使用api中的用户列表内容)、组的所有成员,然后使用Linq搜索正确的结果集。。。。这对于开发/测试来说很好,但在有几百个用户的生产环境中,这将是疯狂的

如有任何意见或建议,将不胜感激。谢谢

编辑

下面是从客户端Javascript调用的代码,用于搜索用户

  • AccessGroupId是用于授权用户的Azure广告组。只有 此组的成员可以访问我自定义处理的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);
}

我要强调两种可能的方法:

  • 使用自定义JS库执行对Graph API的请求。 您仍然需要关注AccessToken,并查看ADAL.js
  • 示例应用程序(截至撰写本文时尚未定稿)可从以下网址获得:

    请查看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);        
    
    公共异步任务FindUser(字符串前置){
    ActiveDirectoryClient=AADHelper.GetActiveDirectoryClient();
    IPagedCollection pagedCollection=await client.Users.Where(u=>u.UserPrincipalName.StartsWith(pre,StringComparison.CurrentCultureIgnoreCase)).ExecuteAsync();
    如果(pagedCollection!=null)
    {
    做
    {
    List usersList=pagedCollection.CurrentPage.ToList();
    foreach(用户列表中的IUser用户)
    {
    userList.Add((User)User);
    }
    pagedCollection=等待pagedCollection.GetNextPageAsync();
    }while(pagedCollection!=null);
    }
    返回Json(userList,JsonRequestBehavior.AllowGet);
    
    }

    有关更详细的样本,请访问:

    这是不久前提出的问题。我很好奇你是否找到了解决办法。我正在努力用一个完全在Azure中托管的网站取代基于SharePoint的网站。我以为我在Azure广告上看到了一个“人物选择器”样本,但似乎找不到它。我确实找到了Vitorrio(Azure AD PM)的Org Navigator:正在寻找与Org Navigator类似的功能,但希望搜索目录中所有用户的部分匹配项,以便在用户在文本框中键入搜索词时筛选结果。我看到这是一条旧评论,但我还是要问:你从哪里获得AADHelper?这是我从Nuget下载的Microsoft.Azure.ActiveDirectoryGraphClient中不再包含的内容吗?还是定制的?
    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);