1

I am building the admin side of the project at the moment where I will have to : - create new users and add some more specific information about them (i.e. location, position etc.) - different users will have to be assigned to different roles,

What I wanted to do first was to pull the information of existing users inner join the data with the information in the additional table and filter by different roles

public class UserDetail
{
    [HiddenInput(DisplayValue = false)]
    public string UserId { get; set; }

    public int StoreID { get; set; }

    public string PositionID { get; set; }
}

In the controller, I wanted to do a simple thing at first to inner join the data from Membership class and inner join with the above UserDetail class at UserID level. So I used the following piece of code

    public ActionResult Index()
    {
        var allSystemUsers = Membership.GetAllUsers().Cast<MembershipUser>()
            .Select(user => new {user.UserName, user.Email, user.CreationDate, user.LastLoginDate });

                    return View();
    }

The problem is that I don't know how to pull first the people in certain roles only instead all of them and then how to inner join with the above UserDetail class.

Once I have Index ActionaResult I hope I should be able to create - Create and Edit functionality

Observer
  • 11
  • 2

2 Answers2

0

You don't have to just use .Select as you can also use .Skip and .Take. You can also use .OrderBy to set a repeatable order, and also use .Where to filter out the users you don't want to include.

That's if you want to roll your own.

Now, it may not be exactly what you are looking for if you really want to roll your own, but Troy Goode has written a most excellent, area-based approach for membership. You could fork it off of GitHub and alter it to use profiles if you need (or bind additional information to users through EF Code First, I've done that).

https://github.com/TroyGoode/MembershipStarterKit

This is a great starting point and handles most of user management for you off the hop. As it's put into an area, it has a minimal impact on the rest of your site.

MisterJames
  • 3,306
  • 1
  • 30
  • 48
0

To get users in a particular role, use the Role provider:

List<string> users = Roles.GetUsersInRole("someRole");

This will return a list of usernames of all users assigned to the specified role. Then you can iterate over the users list and grab whatever details you want.

foreach(var user in users)
{
   //do stuff
} 

A slightly easier approach (imho) for storing the UserDetail would be to use the Profile provider included with the mvc3 framework. Then you would be using the three providers (Membership, Roles and Profiles) hand in hand as they were designed to be used.

Forty-Two
  • 7,535
  • 2
  • 37
  • 54