locked
Asp.Net MVC Idenitty how to get users where role is .... ? RRS feed

  • Question

  • User-1949524191 posted

    Hi All

    pls help me, how to get all users who belong to appropriate role? Best via UserManager or other methods included in AccountController - created during installing this tool?

    public class AccountController : Controller
        {        
    
            private ApplicationSignInManager _signInManager;
            private ApplicationUserManager _userManager;
    
            ApplicationDbContext db = new ApplicationDbContext();
    
            public AccountController()
            {
            }
    
            public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
            {
                UserManager = userManager;
                SignInManager = signInManager;
            }
    
            public ApplicationSignInManager SignInManager
            {
                get
                {
                    return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
                }
                private set
                {
                    _signInManager = value;
                }
            }
    
            public ApplicationUserManager UserManager
            {
                get
                {
                    return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
                }
                private set
                {
                    _userManager = value;
                }
            }
    ......

    Thank you in advance.

    BRgds

    Chris

    Wednesday, August 24, 2016 4:37 PM

Answers

  • User-2057865890 posted

    Hi kszymaniak,

    You could use linq statement to get all users who belongs to appropriate role.

    public class AccountController : Controller
    {
        private ApplicationUserManager _userManager;
        private ApplicationSignInManager _signInManager;
        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
        {
            UserManager = userManager;
            SignInManager = signInManager;
        }
        public ActionResult UserList()
        {
            var applicationDbContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
            var users = from u in applicationDbContext.Users
                        from ur in u.Roles
                        join r in ApplicationDbContext.Roles on ur.RoleId equals r.Id
                        select new
                        {
                            u.Id,
                            Name = u.UserName,
                            Role = r.Name,
                        };
                // users is anonymous type, map it to a Model 
                return View(users);
        }
    }

    Please refer to the following link.

    Getting a list of users with their assigned role in Identity 2

    http://stackoverflow.com/questions/26078271/getting-a-list-of-users-with-their-assigned-role-in-identity-2

    Best Regards,
    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 25, 2016 9:36 AM
  • User-1949524191 posted

    I am sorry, but I wonder that you badly understood my question. I want to get only users which belong to role "Admin" example: select users where role is "Admin". How to do it? I am sorry for misunderstanding.

    BRgds

    Update: I found solution, see link: http://blog.falafel.com/check-user-exists-role-asp-net-identity-2-0/

    a little adjustments and can be

    Chris, thank you for great code for listing all users with their roles

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 25, 2016 7:42 PM

All replies

  • User-491950272 posted

    Greetings,

    You might need to use IQueryable<> to achieve the required functionality. Try the following code:

    public IQueryable<User> GetUsersInRole(string roleName)
    {
        return from user in Users
               where user.Roles.Any(r => r.Role.Name == roleName)
               select user;
    }

    Thursday, August 25, 2016 8:03 AM
  • User-1949524191 posted

    Thanks Janshair

    but I got error that "User" is a "property but used like a "type" and "Users" is not found.

    Rgds

    Thursday, August 25, 2016 9:32 AM
  • User-2057865890 posted

    Hi kszymaniak,

    You could use linq statement to get all users who belongs to appropriate role.

    public class AccountController : Controller
    {
        private ApplicationUserManager _userManager;
        private ApplicationSignInManager _signInManager;
        public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
        {
            UserManager = userManager;
            SignInManager = signInManager;
        }
        public ActionResult UserList()
        {
            var applicationDbContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
            var users = from u in applicationDbContext.Users
                        from ur in u.Roles
                        join r in ApplicationDbContext.Roles on ur.RoleId equals r.Id
                        select new
                        {
                            u.Id,
                            Name = u.UserName,
                            Role = r.Name,
                        };
                // users is anonymous type, map it to a Model 
                return View(users);
        }
    }

    Please refer to the following link.

    Getting a list of users with their assigned role in Identity 2

    http://stackoverflow.com/questions/26078271/getting-a-list-of-users-with-their-assigned-role-in-identity-2

    Best Regards,
    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 25, 2016 9:36 AM
  • User-1949524191 posted

    I am sorry, but I wonder that you badly understood my question. I want to get only users which belong to role "Admin" example: select users where role is "Admin". How to do it? I am sorry for misunderstanding.

    BRgds

    Update: I found solution, see link: http://blog.falafel.com/check-user-exists-role-asp-net-identity-2-0/

    a little adjustments and can be

    Chris, thank you for great code for listing all users with their roles

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 25, 2016 7:42 PM