How to obtain a list of Users from ASP.NET Identity?
For RTM, you will have to drop down to your DbContext
or whatever your specific store implementation has to enumerate all users. In the next release, we will most likely be adding an optional IQueryable
Users/Roles method on the Manager classes that stores can implement to expose IQueryables
for both users and stores.
I found out that I wasn't using the derived ApplicationUser
object for anything, so I just went ahead and changed all uses of it for plain old User
. Then I just changed ApplicationDbContext
definition for the following:
public class ApplicationDbContext : IdentityDbContext<
User, UserClaim, UserSecret, UserLogin,
Role, UserRole, Token, UserManagement>
{
}
And now I can access the user list:
UsersContext = new ApplicationDbContext();
...
UsersContext.Users.ToList();
However, I think this will come back and haunt me in the future (I'll probably need to add more fields to User
) so probably I'll have to use the same approach as in this question:
Get all role names in ASP.NET MVC5 Identity system
Edit: Since I got the need to add a new property, I had to revert my changes. So I went ahead and did a line by line comparison with the ASP.NET Identity Sample Project, and found out that the generated project had the following line:
IdentityManager = new AuthenticationIdentityManager(new IdentityStore());
while the Sample application had included the database context in the constructor. So I added it in my constructor, recreated the database and the problem went away.
IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
- Create
ASP .NET MVC5
project by default - Create
ASP .NET Identity
tables properly and change connection string as well. - To get users just do the following test
A. Go to
AccountController
B. Create any dummy method and put there
var context = new ApplicationDbContext(); var allUsers = context.Users.ToList();