Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 645
Leonardo Herrera Avatar asked Sep 12 '13 20:09

Leonardo Herrera


People also ask

How do you get user role in identity?

it gives you the AspNetUserInRoles which stores UserId and RoleId. Instead you could try UserManger 's GetRoles method which will return you List<string> of roles user is assigned. But as you mentioned it will be only one role hence you can take first value from the result of GetRoles method.

Which method enables you to return a list of users in a role that has a particular userName?

The following code example uses the GetUsersInRole method to get a list of the users in a particular role and binds the results to a GridView control.

How do I find my userName by identity .NET core?

You can create a method to get the current user : private Task<ApplicationUser> GetCurrentUserAsync() => _userManager. GetUserAsync(HttpContext. User);


4 Answers

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()));
like image 111
Leonardo Herrera Avatar answered Sep 25 '22 06:09

Leonardo Herrera


  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

like image 27
Friend Avatar answered Sep 22 '22 06:09

Friend


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.

like image 43
Hao Kung Avatar answered Sep 21 '22 06:09

Hao Kung


using System.Linq;
using System.Data;
using System.Data.Entity;

        var db = new ApplicationDbContext();
        var Users = db.Users.Include(u => u.Roles);
like image 44
Hossein Hajizadeh Avatar answered Sep 22 '22 06:09

Hossein Hajizadeh