How to obtain a list of Users from ASP.NET Identity?

Leonardo Herrera picture Leonardo Herrera · Sep 12, 2013 · Viewed 76.6k times · Source

Edit: This question is outdated

The Identity Framework was a moving target at the moment I asked this. The authors changed quite a few things and they have decoupled several others, making everything easier.

Have a look at the Asp.NET Identity Sample project on github.


I'm creating a small application that requires user management. Registration is not allowed, instead there is a super user that will create and modify login information.

I'm using the new ASP.NET Identity membership system, and sure enough, creating users and adding roles is easy and intuitive.

Now, my question: How to obtain a list of users using the AuthenticationIdentityManager class that is used by the generated AccountController class? I couldn't find a way to access the user list from my controller.

(By the way, the new name "Identity" may sound awesome to some people but it is a pain to search for.)

Edit: If I try to do this

ApplicationDbContext UsersContext = new ApplicationDbContext();
UsersContext.Users.ToList(); // Exception

I get an exception Invalid column name 'Discriminator'. The definition of ApplicationDbContext is generated automatically by the new application wizard:

using Microsoft.AspNet.Identity.EntityFramework;

namespace Cobranzas.Models
{
    public class ApplicationUser : User
    {

    }
    public class ApplicationDbContext : IdentityDbContextWithCustomUser<ApplicationUser>
    {

    }
}

So my guess is that Discriminator column is for telling apart ApplicationUser from User. However, it does not exists in my database (which was created automatically by the application.)

Answer

Leonardo Herrera picture Leonardo Herrera · Sep 13, 2013

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