Add new user creation
This commit is contained in:
@ -10,46 +10,12 @@ namespace Hcs.WebApp.Controllers
|
||||
[Route("identity/")]
|
||||
[Authorize]
|
||||
public class IdentityController(
|
||||
IUserStore<AppUser> userStore,
|
||||
UserManager<AppUser> userManager,
|
||||
SignInManager<AppUser> signInManager) : Controller
|
||||
{
|
||||
private readonly IUserStore<AppUser> userStore = userStore;
|
||||
private readonly UserManager<AppUser> userManager = userManager;
|
||||
private readonly SignInManager<AppUser> signInManager = signInManager;
|
||||
|
||||
[HttpPost]
|
||||
[Route("register")]
|
||||
[AllowAnonymous]
|
||||
public async Task<IActionResult> Register(string userName, string password, string returnUrl)
|
||||
{
|
||||
var user = Activator.CreateInstance<AppUser>();
|
||||
await userStore.SetUserNameAsync(user, userName, CancellationToken.None);
|
||||
|
||||
var result = await userManager.CreateAsync(user, password);
|
||||
if (!result.Succeeded)
|
||||
{
|
||||
var error = string.Join(", ", result.Errors.Select(x => x.Description));
|
||||
if (!string.IsNullOrEmpty(returnUrl))
|
||||
{
|
||||
return Redirect($"/account/register?error={error}&returnUrl={Uri.EscapeDataString(returnUrl)}");
|
||||
}
|
||||
else
|
||||
{
|
||||
return Redirect($"/account/register?error={error}");
|
||||
}
|
||||
}
|
||||
|
||||
await signInManager.SignInAsync(user, isPersistent: false);
|
||||
|
||||
if (string.IsNullOrEmpty(returnUrl))
|
||||
{
|
||||
return Redirect("/");
|
||||
}
|
||||
|
||||
return Redirect(returnUrl);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Route("login")]
|
||||
[AllowAnonymous]
|
||||
|
||||
Reference in New Issue
Block a user