Getting all ids and usernames from membership Asp Net MVC - asp.net-mvc

I have no doubt on how to retrieve membership id from 1 single user:
currentUser = Membership.GetUser();
Guid currentUserId = (Guid)currentUser.ProviderUserKey;
but I can´t find a way to retrieve all users id and usernames.
I tried this way:
MembershipUserCollection AllUsers;
AllUsers = GetAllUsers();
From that point I can´t find a way to retrieve Ids and Usernames from AllUsers.
Could I get some help from here?

I think you're looking for Membership.GetAllUsers()
var users = Membership.GetAllUsers();
foreach (MembershipUser membershipUser in users)
{
// membershipUser.UserName;
}
http://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.getallusers.aspx

Related

Get Membership Users and Emails

I find it difficult to understand the structure of the MembershipUserCollection class. For example, I get All Users as a MembershipUserCollection, yet I cannot access MembershipUser to get the users emails. Here is a sample of what I currently do:
MembershipUserCollection AllUsers = Membership.GetAllUsers();
foreach (var user in AllUsers)
{
var userName = user.ToString();
// Is there a better way to get the email within loop without having to call the GetUser method??
var email = Membership.GetUser(user).Email
}
Try using
foreach (MembershipUser user in AllUsers)

MVC Routing Access

Hi I would like to know whether this can be done in MVC.
Lets say we have customer order entry of
Customer ID - 1
Order ID - 2
Let's say when user types /Order/View?id=2
I would like this page can be accessible to customer who has ID 1 only. Let's assume we keep this ID in identity.
Is there easy way to achieve this ?
Thanks,
public ActionResult View(){
var userName = User.Identity.Name;
var id = Request["id"];
if (userName == "1" && id == "2")
return RedirectToAction("Authorized");
else
return RedirectToAction("Unauthorized");
}

Using ASP.Net Membership, get all users

I'm trying to use Membership.GetAllUsers() to create a list of all of my users in the database. However, I have a model that I use that maps out the user properties (First Name, Last Name, Email, etc.).
How can I add all of the users into List<ManageUserViewModel>.
I've already tired:
List<ManageUserViewModel> model = Membership.GetAllUsers();
and then
MembershipUserCollection users = Membership.GetAllUsers();
List<ManageUserViewModel> model = new List<ManageUserViewModel>();
foreach (var item in users)
{
model.Add(item);
}
If you're explicit with the object type in the foreach loop, you'll be able to access the user object you're looking for.
For example:
var users = Membership.GetAllUsers();
var userList = new List<MembershipUser>();
foreach (MembershipUser user in users)
{
userList.Add(user);
}
Membership.GetAllUsers() returns a MembershipUserCollection which in practice is a list of MembershipUser, whereas you want a list of ManageUserViewModel which I assume is an internal class to your application.
You can use LINQ for this:
var model = Membership.GetAllUsers()
.Select(m =>
new ManageUserViewModel {/* set properties you need here */ }
)
.ToList();
Which is the equivalent of:
var users = Membership.GetAllUsers();
var model = new List<ManageUserViewModel>();
foreach (var item in users)
{
model.Add(new ManageUserViewModel { /* set properties here */});
}
I had the same challenge. mine was with vb not c# and .NEt version 4.5 using visual studio 2013.
I got all the solution from Microsoft website here
best of luck

SimpleMembership MVC 4 get data from UserProfile having role in webpages_Roles

I have the following code. But there must be a more database efficient way to do this, because, correct me if Im wrong the number of database queries is
(1 + (qty of users with admin role))
With a new MVC 4 project the membership defaults are the tables UserProfile, webpages_Roles and webpages_UsersInRoles. I see lots of built in methods for Roles.Get*. If I want to avoid writing code like what I have below do I need to explicitly create a model for webpages_Roles and webpages_UsersInRoles as well as all the code first properties? Getting just the username from Roles.Get* doesnt suffice, I need the full UserProfile.
FYI the "UserRole" object below is just an enum
public ActionResult Admins()
{
var dbContext = new UsersContext();
var usernames = Roles.GetUsersInRole(UserRole.SiteAdministrator.ToString());
var adminUsers = new List<UserProfile>();
foreach (string username in usernames)
{
var adminUser = dbContext.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == username);
adminUser.Roles.Add(UserRole.SiteAdministrator);
adminUsers.Add(adminUser);
}
return View(adminUsers);
}
Yes, you don't want to do it that way. That's a very inefficient way.
Instead, use the tools as they were designed. For example, something like this:
var usernames = Roles.GetUsersInRole(UserRole.SiteAdministrator.ToString());
var adminUsers = dbContext.UserProfiles
.Where(x => usernames.Contains(x.Username)).ToList();
return View(adminUsers);

ASP.NET MVC2 Membership: How to get userID and roleID of logged user?

How can I get userID and roleID of user that is logged to application? User.Identity doesn't contain these details?
Thanks,
Ile
Here's how:
string userId = Membership.GetUser().ProviderUserKey.ToString();
string[] roleNames = Roles.GetRolesForUser(username);
Another good one to know is:
bool isAdmin = Roles.IsUserInRole("Admin");
You can use this too :
List<String> roles = Roles.GetRolesForUser().ToList();

Resources