【问题标题】:Get all users and their roles they got into a table asp.net mvc获取所有用户及其角色进入表 asp.net mvc
【发布时间】:2016-04-28 10:46:40
【问题描述】:

我试图弄清楚如何将所有用户及其角色放入一个井井有条的表格中,看到了许多不同的方法,但没有一个对我有用。至少我已经在路上了……有人可以帮助我吗?

有了这段代码,我得到了所有的角色:

        var context = new ApplicationDbContext();
        var roleStore = new RoleStore<IdentityRole>(context);
        var roleManager = new RoleManager<IdentityRole>(roleStore);
        var roles = (from r in roleManager.Roles select r.Name).ToList();
        ViewBag.Roles = roles.ToList();

通过这段代码,我得到了所有注册用户:

        var context = new ApplicationDbContext();
        var userStore = new UserStore<ApplicationUser>(context);
        var userManager = new UserManager<ApplicationUser>(userStore);
        var users = (from u in userManager.Users select u.UserName).ToList();
        ViewBag.Users = users.ToList();

最后用这段代码我得到了所有用户的ID

        var context = new ApplicationDbContext();
        var user = (from u in userManager.Users select u.Id).ToList();
        ViewBag.id = user.ToList();

但是我如何让所有用户获得他们的角色?实在想不通

【问题讨论】:

  • 你有用户和角色之间的映射表吗?
  • @AleksandrIvanov 我不这么认为,但是在我作为管理员的角色视图中,我可以检查用户拥有的角色等,遵循本指南andersnordby.wordpress.com/2014/11/28/… 进行一些细微的更改

标签: c# asp.net asp.net-mvc user-roles


【解决方案1】:

这是获取用户列表(带有用户名)和相关角色列表的一个选项。

控制器:

public ActionResult GetRoles()
        {
            var userRoles = new List<RolesViewModel>();
            var context = new ApplicationDbContext();
            var userStore = new UserStore<ApplicationUser>(context);
            var userManager = new UserManager<ApplicationUser>(userStore);

            //Get all the usernames
            foreach (var user in userStore.Users)
            {
                var r = new RolesViewModel
                {
                    UserName = user.UserName
                };
                userRoles.Add(r);
            }
            //Get all the Roles for our users
            foreach (var user in userRoles)
            {
                user.RoleNames = userManager.GetRoles(userStore.Users.First(s=>s.UserName==user.UserName).Id);
            }

            return View(userRoles);
        }

视图模型

public class RolesViewModel
{
    public IEnumerable<string> RoleNames { get; set; }
    public string UserName { get; set; }
}

视图:简单的一个

@model IEnumerable<StackOverflow.Models.RolesViewModel>
@foreach (var user in Model.ToList())
{
    <div> @user.UserName -- </div>
    foreach (var role in user.RoleNames)
    {
        <div> @role </div>
    }
    <br />
}

【讨论】:

    【解决方案2】:

    我相信您正在使用 AspNetUserRoles 表来管理用户和角色。 您可以使用 EF 和 Linq 在网格中优化用户/角色的显示 -

    [GridAction]
    public ActionResult _GetUsers()
    
    {
        var users = from user in xcsnEntities.Users
                    select new
                    {
                        Role = user.Roles.FirstOrDefault().RoleName,
                        IsApproved = user.Membership.IsApproved,
                        Email = user.Membership.Email,
                        UserName = user.UserName
                    };
    
        return View(new GridModel(users));
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 1970-01-01
      • 2023-04-04
      • 2016-12-26
      • 2013-10-29
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多