添加用户查询功能并更新注册页面和控制器方法

This commit is contained in:
罗澜大帅哥 2025-07-12 15:00:55 +08:00
parent 0f859cf39e
commit c21b8be5e1
6 changed files with 80 additions and 9 deletions

View File

@ -12,6 +12,11 @@
<h2>创建新账户。</h2>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger" role="alert"></div>
<div class="form-floating mb-3">
<input asp-for="Input.UserName" class="form-control" autocomplete="username" aria-required="true" placeholder="luolan" />
<label asp-for="Input.UserName">用户名</label>
<span asp-validation-for="Input.UserName" class="text-danger"></span>
</div>
<div class="form-floating mb-3">
<input asp-for="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="name@example.com" />
<label asp-for="Input.Email">电子邮件</label>

View File

@ -110,9 +110,12 @@ namespace AGSS.Areas.Identity.Pages.Account
[MaxLength(10)]
[Display(Name = "Confirm password")]
public string Sex { get; set; }
[MaxLength(10)]
public string UserName { get; set; }
}
@ -131,7 +134,7 @@ namespace AGSS.Areas.Identity.Pages.Account
var user = CreateUser();
user.Id = Guid.NewGuid().ToString();
user.Sex = Input.Sex;
await _userStore.SetUserNameAsync(user, Input.Email, CancellationToken.None);
await _userStore.SetUserNameAsync(user, Input.UserName, CancellationToken.None);
await _emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None);
var result = await _userManager.CreateAsync(user, Input.Password);

View File

@ -174,7 +174,7 @@ namespace AGSS.Controllers.Admin
return Ok(new ReturnTemplate(200, "删除子级字典成功", null));
}
[HttpGet]
[HttpPost]
public IActionResult GetChildDictionaries([FromBody] ChildDictionaryRequest request)
{
if (string.IsNullOrWhiteSpace(request.Value))

View File

@ -169,8 +169,13 @@ public class AdminRoleControllers:ControllerBase
}
[HttpGet]
public async Task<IActionResult> AllRole()
{
return Ok(new ReturnTemplate(200,"查询成功啦!", _roleManager.Roles.ToList()));
}
@ -189,11 +194,12 @@ public class AdminRoleControllers:ControllerBase
IList<UserProfile> usersInRole = null;
if (string.IsNullOrWhiteSpace(request.RoleName))
{
usersInRole = _userManager.Users.ToList();
usersInRole = await _userService.GetUsersProfileByUserNameAsync(request.UserName);
}
else
{
usersInRole = await _userService.GetUsersProfileInRoleAsync(request.RoleName);
var usersInRole1 = await _userService.GetUsersProfileInRoleAsync(request.RoleName);
usersInRole = usersInRole1.Where(a => a.UserName.Contains(request.UserName)).ToList();
}
@ -220,6 +226,7 @@ public class AdminRoleControllers:ControllerBase
{
public string UserName { get; set; }
/// <summary>
/// 表示角色的名称。此属性用于指定或获取与用户管理相关的角色名称。
/// 在进行角色分配、查询等操作时,需要提供正确的角色名称以确保操作的成功执行。

View File

@ -5,8 +5,7 @@ namespace AGSS.Models.Entities;
public class UserModel:IdentityUser<string>
{
public string? Sex { get; set; }
public string? Sex { get; set; }
[MaxLength(100)]
public string? Description { get; set; }
[MaxLength(200)]

View File

@ -1,6 +1,7 @@
using AGSS.Models;
using AGSS.Models.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace AGSS.Services;
@ -69,4 +70,60 @@ public class UserService
// For now, returning the first user's profile
return userProfiles;
}
public async Task<List<UserProfile>> GetUsersProfileByUserNameAsync(string userName)
{
var users = await _userManager.Users
.Where(u => u.UserName.Contains(userName))
.Select(u => new UserProfile
{
Id = u.Id,
UserName = u.UserName,
Email = u.Email,
Sex = u.Sex,
Description = u.Description,
Config = u.Config,
JobCode = u.JobCode,
JobName = u.JobName,
Birthday = u.Birthday,
MenuCode = u.MenuCode,
MenuName = u.MenuName
})
.ToListAsync();
if (users == null || !users.Any())
{
throw new ArgumentException("No users found with the specified username");
}
return users;
}
public async Task<List<UserProfile>> GetUsersProfileAllAsync()
{
var users = await _userManager.Users
.Select(u => new UserProfile
{
Id = u.Id,
UserName = u.UserName,
Email = u.Email,
Sex = u.Sex,
Description = u.Description,
Config = u.Config,
JobCode = u.JobCode,
JobName = u.JobName,
Birthday = u.Birthday,
MenuCode = u.MenuCode,
MenuName = u.MenuName
})
.ToListAsync();
if (users == null || !users.Any())
{
throw new ArgumentException("No users found");
}
return users;
}
}