32

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

David Dury
  • 5,537
  • 12
  • 56
  • 94

1 Answers1

59

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();
    }
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 4
    Perfect! You opened my eyes! I wass missing a reference to `System.Data.Entity` in order to get the `ToListAsync()` extension method. Great, works perfect ... – David Dury Oct 14 '14 at 10:28
  • 1
    "There is no way to do this asynchronously with the UserManager class directly"... your second example is exactly how the API designers intended it to be called. – Paul Suart Nov 19 '18 at 16:03