如何根据角色在ASP.NET Core 6中创建用户列表

huangapple go评论55阅读模式
英文:

How to create a list of users based on the roles in ASP.NET Core 6

问题

I have created a model for my users which inherits from IdentityUser. Here is my model:

 public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FatherName { get; set; }
        public string TelephoneNO { get; set; }
        public string Address { get; set; }
        public JobTitle JobTitle { get; set; }
        public Degree Degree { get; set; }
        
        //Relationships
        public string? UserId { get; set; }
        public virtual ApplicationUser User { get; set; }
        public int? ClassroomId { get; set; }
        public Classroom Classroom { get; set; }

        public virtual ICollection<Homework> Homeworks { get; set; }
        public virtual ICollection<UserMajorCourse> UsersMajorsCourses { get; set; }
        public virtual ICollection<UserHomework> UsersHomeworks { get; set; }
    }
}

I want to list the users based on their roles. Here is my role class:

 public static class UserRoles
    {
        public const string Admin = "Admin";
        public const string Teacher = "Teacher";
        public const string Student = "Student";
    }

I tried using the following method, but I received a null exception error.

var allStudents = _userManager.GetUsersInRoleAsync("Student");

How can I get the list of users based on their roles?

英文:

I have created a model for my users which inherits from IdentityUser.
Here is my model:

 public class ApplicationUser:IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FatherName { get; set; }
        public string TelephoneNO { get; set; }
        public string Address { get; set; }
        public JobTitle JobTitle { get; set; }
        public Degree Degree { get; set; }
        
        //Relationships
        public string? UserId { get; set; }
        public virtual ApplicationUser User { get; set; }
        public int? ClassroomId { get; set; }
        public Classroom Classroom { get; set; }

        public virtual ICollection&lt;Homework&gt; Homeworks { get; set; }
        public virtual ICollection&lt;UserMajorCourse&gt; UsersMajorsCourses { get; set; }
        public virtual ICollection&lt;UserHomework&gt; UsersHomeworks { get; set; }
        }
    }
}

I want to list the users based on their roles.
Here is my role class:

 public static class UserRoles
    {
        public const string Admin = &quot;Admin&quot;;
        public const string Teacher = &quot;Teacher&quot;;
        public const string Student = &quot;Student&quot;;

    }

I tried using the following method, but I received a null exception error.

var allStudents = _userManager.GetUsersInRoleAsync(&quot;Student&quot;);

How can I get the list of users based on their roles?

答案1

得分: 2

您可以在用户管理器中获取所有用户,然后根据他们的角色进行排序。类似于以下代码示例:

public async List<ApplicationUser> GetUsersInRole(string role)
{
    var allUsers = await _userManager.Users.ToListAsync();
    var usersInRole = new List<ApplicationUser>();
    foreach (ApplicationUser user in allUsers)
    {
         var roles = await _userManager.GetRolesAsync(user);
         if (roles.Contains(role))
              usersInRole.Add(user);
    }

    return usersInRole;
}
英文:

You can get all users in the user manager and then sort through them based on their roles. Similar to something like this.

public async List&lt;ApplicationUser&gt; GetUsersInRole(string role)
{
    var allUsers = await _userManager.Users.ToListAsync();
    var usersInRole = new List&lt;ApplicationUser&gt;();
    foreach (ApplicationUser user in allUsers)
    {
         var roles = await _userManager.GetRolesAsync(user);
         if (roles.Contains(role))
              usersInRole.Add(user);
    }

    return usersInRole;
}

huangapple
  • 本文由 发表于 2023年6月29日 20:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定