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任何幫助,你可以給我,謝謝。
如果您只從Azure AD獲得1位用戶,該怎麼辦? – stuartd
您應該能夠觀看http流量並從響應中獲得更明確的錯誤消息。 –
@stuartd - 有1或最多100個用戶可以使用CurrentPage方法,不需要GetNextPageAsync(),如果我不想要更多。 –