2014-10-06 84 views
3

我想在我的asp.net mvc 5應用程序中使用自動完成功能來搜索某個特定Azure AD組中的用戶,從而具有某種人員選擇器功能。這是一個演示「todo應用程序」,它允許爲作爲該組成員的用戶分配待辦事項。使用圖形API在Azure Active Directory組中搜索用戶

我試圖直接使用圖形API和Azure圖形客戶端庫,但我似乎沒有找到一種方法來實現我想要的。圖api允許獲取組的成員,但添加過濾器「startswith」失敗,因爲在添加過濾器時,api只返回不包含例如DisplayName屬性的目錄對象...客戶端庫也沒有多大幫助除了批量功能提供了一種方式,但有很多開銷......然後我必須得到用戶的過濾結果集,而不管組成員(在api中使用用戶列表的東西),然後是組的所有成員釣魚使用Linq的正確結果集....將工作正常開發/測試,但在生產與幾百用戶這將是瘋了...

任何意見或建議將不勝感激。謝謝!

編輯

我下面的代碼是從客戶端的JavaScript調用搜索用戶;

  • AccessGroupId是用於授權用戶的Azure AD組。只有 該組的成員可以訪問Web應用程序,我在定製處理 OWin中間件
  • 的方法intented被用來找到該組中的用戶

代碼工作正常,如下只存在沒有應用濾波,這是輸入參數pre(來自ui中的文本框)的intent。我獲得了訪問組的所有成員。

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調用,而不是返回成員(用戶,組和/或聯繫人)的,它返回目錄的對象(即不具有顯示名),這是不是真的有用的上面的代碼,除非我再次查詢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); 
+0

Th是前一陣子被問到的。好奇,如果你找到了解決辦法。 我正在使用一個完全在Azure中託管的站點替換基於SharePoint的站點。我以爲我曾在Azure AD的某個地方看過一個樣本「人員挑選器」,但似乎無法找到它。 我確實找到了Vitorrio's(Azure AD PM)組織導航器:http://www.cloudidentity.com/blog/org-navigator/ 尋找與組織導航器類似的功能,但想要搜索部分匹配目錄中的用戶,以便我可以在用戶在文本框中鍵入搜索條件時過濾搜索結果。 – Paul 2015-06-03 20:23:46

回答

1

我想強調兩種可能的方法:

  1. 執行請求圖形API使用自定義JS庫。 您將需要仍然需要照顧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

    相關問題