ASP.NET身份2的UserManager得到所有用户异步 [英] ASP.NET Identity 2 UserManager get all users async

查看:564
本文介绍了ASP.NET身份2的UserManager得到所有用户异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我们,如果有一种方式来获得ASP.NET中的所有用户异步身份2?

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

UserManager.Users 没有什么异步或发现所有异步或somwething像

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

推荐答案

有没有办法做到这一点异步与的UserManager 类直接。你可以把它包在自己的异步方法:(这可能是有点邪恶的)

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

ToListAsync 扩展方法:

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

或者直接使用上下文:

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

这篇关于ASP.NET身份2的UserManager得到所有用户异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆