Add new user creation

This commit is contained in:
2025-10-19 19:09:44 +09:00
parent 4b6f626f29
commit 42c0509978
14 changed files with 710 additions and 157 deletions

View File

@ -0,0 +1,40 @@
using Hcs.WebApp.Data;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
namespace Hcs.WebApp.Services
{
public class UsersService(IDbContextFactory<AppIdentityDbContext> factory, UserManager<AppUser> userManager)
{
private readonly IDbContextFactory<AppIdentityDbContext> factory = factory;
private readonly UserManager<AppUser> userManager = userManager;
public async Task<IEnumerable<AppUserWithRole>> GetUsersWithRole()
{
using var context = factory.CreateDbContext();
return await (from user in context.Users
join userRole in context.UserRoles on user.Id equals userRole.UserId
join role in context.Roles on userRole.RoleId equals role.Id
select new AppUserWithRole()
{
User = user,
Role = role
}).ToListAsync();
}
public async Task<IdentityResult> CreateUser(string userName, string roleName, string password)
{
var user = new AppUser()
{
UserName = userName,
NormalizedUserName = userName.Normalize()
};
var result = await userManager.CreateAsync(user, password);
if (result.Succeeded)
{
result = await userManager.AddToRolesAsync(user, [roleName]);
}
return result;
}
}
}