简体   繁体   English

Azure 活动目录获取所有用户 - (可能会死锁)

[英]Azure active directory get all users - (possibly deadlock)

I have 150 users in Azure Active directory and I'm getting them this way:我在 Azure 活动目录中有 150 个用户,我是这样获取他们的:

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

And this is the AuthenticationHelper Class:这是 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;
    }
}

So the problem is that it works perfect in local but after deploying it on Azure Web App and executing the service that calls GetUsersAAD() method, doesn't work, the http request freezes and after some minutes I get a 500 error with timeout.所以问题是它在本地工作得很好,但是在 Azure Web App 上部署它并执行调用 GetUsersAAD() 方法的服务后,它不起作用,http 请求冻结,几分钟后我收到 500 错误并超时。

This is a WebApi2 proyect on AspNet MVC.这是一个基于 AspNet MVC 的 WebApi2 项目。

So I really would apreciate any help you can give me, thanks.所以我真的很感激你能给我的任何帮助,谢谢。

Ok, after reading and searching a lot I was able to make it works, it seems that .Result it's prone to deadlocks and It's not recommended on async methods, also I was making bad use of async/await/task callings, so the way I make it works was:好的,经过大量阅读和搜索后,我能够使其正常工作,似乎.Result很容易出现死锁,并且不建议在异步方法上使用它,而且我也没有正确使用异步/等待/任务调用,所以方式我让它工作的是:

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

I had to adjust the method to async Task and the callings to it.我不得不将方法调整为async Task及其调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM