56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using Hcs.WebApp.Data;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Radzen;
|
|
|
|
namespace Hcs.WebApp.Controllers
|
|
{
|
|
[Route("identity/[action]")]
|
|
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]
|
|
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 errors = string.Join(", ", result.Errors.Select(error => error.Description));
|
|
if (!string.IsNullOrEmpty(returnUrl))
|
|
{
|
|
return Redirect($"/account/register?errors={errors}&returnUrl={Uri.EscapeDataString(returnUrl)}");
|
|
}
|
|
else
|
|
{
|
|
return Redirect($"/account/register?errors={errors}");
|
|
}
|
|
}
|
|
|
|
await signInManager.SignInAsync(user, isPersistent: false);
|
|
|
|
if (string.IsNullOrEmpty(returnUrl))
|
|
{
|
|
Redirect("/");
|
|
}
|
|
|
|
return Redirect(returnUrl);
|
|
}
|
|
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
await signInManager.SignOutAsync();
|
|
|
|
return Redirect("/");
|
|
}
|
|
}
|
|
}
|