简体   繁体   English

ASP.Net MVC用户列表及其角色

[英]ASP.Net MVC List of User and their Roles

I'm using ASP.NET MVC Default project. 我正在使用ASP.NET MVC默认项目。 In there we have a database called AspNetRoles (defines roles like Admin, User) 在那里,我们有一个名为AspNetRoles的数据库(定义了诸如Admin,User之类的角色)
角色数据库

then there's a database called AspNetUsers which just defines Users when you register them 然后有一个名为AspNetUsers的数据库,它在您注册用户时定义了用户 用户数据库

and the last and most important is AspNetUserRoles . 最后也是最重要的是AspNetUserRoles When you assign Roles to the User here is where it is placed (and the Id's are hashed) 当您将角色分配给用户时,此处是放置它的位置(并且ID被散列)

UserRole数据库

My question is, how do I get a list of every user on the system and their roles? 我的问题是,如何获得系统上每个用户及其角色的列表? I can't figure out how to start doing this. 我不知道如何开始这样做。 Should I create my own User and Role database and forget about the default one? 我应该创建自己的用户和角色数据库,而忽略默认数据库吗? Or is there a way of creating with the default ones? 还是有一种使用默认值创建的方法?


Also, I would keep in mind that as of now I'm assigning Roles when registering (User by default) and later when I create the Index where I show the list of Users and Roles, I will want to edit them, but this is just a disclaimer. 另外,我要记住,到目前为止,我正在注册时分配角色(默认情况下为User),后来在创建显示用户和角色列表的索引时,我将要对其进行编辑,但这是只是免责声明。 Thank you very much for any type of help. 非常感谢您提供的任何帮助。

There are no built in methods for retrieving the list of users with their roles so you can try to get the user and their role like below (using EF): 没有内置的方法可以检索具有其角色的用户列表,因此您可以尝试按以下方式获取用户及其角色(使用EF):

var list = context.Users        
    .Where(x => x.Roles.Select(a => a.Id))
    .ToList();

Using this you get every users and assigned roles. 使用此工具,您可以获得每个用户和分配的角色。

Update Create a Model like below first: 更新首先,如下所示创建模型:

public class UserRoleInfo
{
    public ApplicationUser User { set; get; }
    public List<string> Roles { set; get; } 
}

Then Your Query would be like (Query Syntax): 然后您的查询将类似于(查询语法):

var listOfUsers = (from u in db.Users
             let query = (from ur in db.Set<IdentityUserRole>()
                          where ur.UserId.Equals(u.Id)
                          join r in db.Roles on ur.RoleId equals r.Id select r.Name)
                          select new UserRoleInfo() {User = u, Roles = query.ToList<string>()})
                         .ToList();

Query above will return Every User With their Roles no matter how many. 上面的查询将返回每个用户及其角色,无论有多少。

UPDATE 更新

Your view would be like: 您的看法如下:

@model UserRoleInfo
    <table>
        <tbody>
@foreach (var user in Model)
{
    var appUser = user.ApplicationUser;
    var roles = user.Roles;

            <tr>
                <td>
                    @appUser.Email
                </td>
                <td>
                    @string.Join(",", roles)
                </td>
            </tr>

}
        </tbody>
    </table>

I would suggest going this way since this will return all users from database as a list with their respective roles attached in the user object. 我建议采用这种方式,因为这会将数据库中的所有用户作为列表返回,并在用户对象中附加各自的角色。 Furthermore it uses eagerly loading on Roles entity, so it will be converted to a join in database. 此外,它在角色实体上急切地加载,因此它将转换为数据库中的联接。

If you want to take only one user and its roles where is just a where condition to add and filter by its id. 如果你想利用只有一个用户及其角色,其中仅仅是一个where的条件来添加和过滤器通过其ID。

var users = _applicationDbContext.Users.Include(x => x.Roles).ToList();
foreach (var applicationUser in users)
{
    var userRoles = applicationUser.Roles;
    //Do something with this user roles
}

to get a list of every user on the system and their roles: 获取系统上每个用户及其角色的列表:

SELECT        UserRoleId, UserId, RoleId, dbo.GetRoleName(RoleId) AS RoleName
FROM            dbo.UserRoles

Function : dbo.GetRoleName - 函数:dbo.GetRoleName-

function [dbo].[GetRoleName](@input int)
Returns Nvarchar(500)
As
Begin
Declare @RoleName nvarchar(500)
Set @RoleName=(select Name from Roles where RoleId=@input)
return @RoleName
End

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM