ASP.NET Identity 2 UserManager get all users async

前端 未结 2 1452
余生分开走
余生分开走 2020-12-16 10:52

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 s

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 11:36

    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> GetUsersAsync
    {
        return await Task.Run(() =>
        {
            return userManager.Users(); 
        }
    }
    

    Or use the ToListAsync extension method:

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

    Or use your context directly:

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

提交回复
热议问题