Mamunahmed33
Mamunahmed33

Reputation: 37

Get a user id by the userName in mvc Identity

I need to get all the users list of a specific role and their IDs.

I have searched a lot to get a userId with the user name but i didn't get one. All of them are saying how to get the logged/current user id.

public ActionResult getAllAMUsers()
{
    var AMUserList = Roles.GetUsersInRole("Account Manager");
    var userList = new List<UserViewModel>(); // property userID & userName

    foreach(var item in AMUserList)
    {
       string userID = User.Identity.getUserID(item); // method like this
       userList.add(new UserViewModel{
           userName = item,
           userID = userID
       })
    }

    return View(userList);
}

Upvotes: 1

Views: 900

Answers (1)

Kinexus
Kinexus

Reputation: 12904

Try this;

var users = Roles.GetUsersInRole("Account Manager").Select(Membership.GetUser).ToList()

Upvotes: 1

Related Questions