2

我在Azure的活動目錄150個用戶,我讓他們這樣說:Azure的活動目錄獲得所有用戶 - (可能死鎖)

public List<Generic.UserAAD> GetUsersAAD() 
    { 
     ActiveDirectoryClient activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication(); 

     IPagedCollection<IUser> usersA = activeDirectoryClient.Users.ExecuteAsync().Result; 
     List<IUser> queryUsers = new List<IUser>(); 
     List<Generic.UserAAD> listUsers = new List<Generic.UserAAD>(); 

     do 
     { 
      List<IUser> queryUsersList = usersA.CurrentPage.ToList(); 
      queryUsers.AddRange(queryUsersList); 
      usersA = usersA.MorePagesAvailable ? usersA = usersA.GetNextPageAsync().Result : null; 
     } while (usersA != null); 

     if (queryUsers.Count > 0) 
     { 
      listUsers = queryUsers.Select(u => new Generic.UserAAD { DName = u.DisplayName, UName= u.UserPrincipalName }).ToList(); 
     } 

     return listUsers; 
    } 

這是AuthenticationHelper類:

public class AuthenticationHelper 
{ 
    public static async Task<string> AcquireTokenAsyncForApplication() 
    { 
     return await GetTokenForApplication().ConfigureAwait(false); 
    } 


    public static ActiveDirectoryClient GetActiveDirectoryClientAsApplication() 
    { 
     Uri servicePointUri = new Uri(Constantes.graphUrl); 
     Uri serviceRoot = new Uri(servicePointUri, Constantes.tenantId); 
     ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, 
      async() => await AcquireTokenAsyncForApplication().ConfigureAwait(false)); 
     return activeDirectoryClient; 
    } 


    public static async Task<string> GetTokenForApplication() 
    { 
     AuthenticationContext authContext = new AuthenticationContext(Constantes.authority, false); 
     ClientCredential clientCred = new ClientCredential(Constantes.clientId, Constantes.appKey); 
     AuthenticationResult authenticationResult = authContext.AcquireTokenAsync(Constantes.graphUrl, 
         clientCred).Result; 
     var token = authenticationResult.AccessToken; 
     return token; 
    } 
} 

所以問題是它在本地完美工作,但在Azure Web App上部署並執行調用GetUsersAAD()方法的服務後無效,http請求凍結,幾分鐘後出現500錯誤時間到。

這是AspNet MVC上的WebApi2項目。

所以我真的會apreciate任何幫助,你可以給我,謝謝。

+0

如果您只從Azure AD獲得1位用戶,該怎麼辦? – stuartd

+1

您應該能夠觀看http流量並從響應中獲得更明確的錯誤消息。 –

+0

@stuartd - 有1或最多100個用戶可以使用CurrentPage方法,不需要GetNextPageAsync(),如果我不想要更多。 –

回答

1

好,閱讀和搜索了很多,我能夠讓它工作,之後似乎。結果這是容易出現死鎖,這不是建議在異步方法,也是我正在不好用異步電動機/等待/任務召喚,所以我做它的工作方式是:

public async Task<List<Generic.UserAAD>> GetUsersAAD() 
{ 
    ActiveDirectoryClient activeDirectoryClient = AuthenticationHelper.GetActiveDirectoryClientAsApplication(); 

    Task<IPagedCollection<IUser>> usersTask = activeDirectoryClient.Users.ExecuteAsync(); 
    IPagedCollection<IUser> usersA = await usersTask; 
    List<IUser> queryUsers = new List<IUser>(); 
    List<Generic.UserAAD> listUsers = new List<Generic.UserAAD>(); 

    do 
    { 
     List<IUser> queryUsersList = usersA.CurrentPage.ToList(); 
     queryUsers.AddRange(queryUsersList); 
     usersA = usersA.MorePagesAvailable ? await usersA.GetNextPageAsync() : null; 
    } while (usersA != null); 

    if (queryUsers.Count > 0) 
    { 
     listUsers = queryUsers.Select(u => new Generic.UserAAD { DName = u.DisplayName, UName= u.UserPrincipalName }).ToList(); 
    } 

    return listUsers; 
} 

我不得不調整方法異步任務和召喚它。

相關問題