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 things, making things 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.)
If we can use the following type of
Constructor
inIdentity
AccountController
.then we can directly used
UserManager
object to get user list likeI 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 oldUser
. Then I just changedApplicationDbContext
definition for the following:And now I can access the user list:
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:
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.
ASP .NET MVC5
project by defaultASP .NET Identity
tables properly and change connection string as well.AccountController
B. Create any dummy method and put thereFor 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 optionalIQueryable
Users/Roles method on the Manager classes that stores can implement to exposeIQueryables
for both users and stores.