英文:
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<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?
答案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<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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论