ASP.NET Identity 2 UserManager get all users async

放肆的年华 提交于 2019-12-21 03:21:29

问题


Can somebody tell if there is a way to get all users async in ASP.NET Identity 2?

In the UserManager.Users there is nothing async or find all async or somwething like that


回答1:


There is no way to do this asynchronously with the UserManager class directly. You can either wrap it in your own asynchronous method: (this might be a bit evil)

public async Task<IQueryable<User>> GetUsersAsync
{
    return await Task.Run(() =>
    {
        return userManager.Users(); 
    }
}

Or use the ToListAsync extension method:

public async Task<List<User>> GetUsersAsync()
{
    using (var context = new YourContext())
    {
        return await UserManager.Users.ToListAsync();
    }
}

Or use your context directly:

public async Task<List<User>> GetUsersAsync()
{
    using (var context = new YourContext())
    {
        return await context.Users.ToListAsync();
    }
}



回答2:


Expanding on DavidG's answer for .NET Core 2.1 you would need to user IdentityUser instead of User, as well as you will have to use your context directly.

public async Task<List<IdentityUser>> GetUsersAsync()
{
    using (var context = new ApplicationDbContext())
    {
        return await context.Users.ToListAsync();
    }
}


来源:https://stackoverflow.com/questions/26357676/asp-net-identity-2-usermanager-get-all-users-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!