44
votes

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.)

6
Have you looked at the sample application? github.com/rustd/AspnetIdentitySample/tree/master/…sam
There is nothing there to get a list of users. I already have a working application, I just need a list of all users.Leonardo Herrera

6 Answers

31
votes

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()));
15
votes
  1. Create ASP .NET MVC5 project by default
  2. Create ASP .NET Identity tables properly and change connection string as well.
  3. 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();

enter image description here

4
votes

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.

2
votes
using System.Linq;
using System.Data;
using System.Data.Entity;

        var db = new ApplicationDbContext();
        var Users = db.Users.Include(u => u.Roles);
0
votes

If we can use the following type of Constructor in Identity AccountController.

public AccountController(ApplicationUserManager userManager,
            ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
      UserManager = userManager;
      AccessTokenFormat = accessTokenFormat;
}

public ApplicationUserManager UserManager
{
     get
     {
          return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
     }
     private set
     {
          _userManager = value;
     }
}

then we can directly used UserManager object to get user list like

var userList= UserManager.Users.ToList();
0
votes

You can do this by explicitly setting right types:

var userStore = new UserStore<IdentityUser>();
var userManager = new UserManager<IdentityUser>(userStore);
IQueryable<IdentityUser> usersQuery = userManager.Users;
List<IdentityUser> users = usersQuery.ToList();

Imoports:

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Linq;
using System.Collections.Generic;