102 lines
2.9 KiB
C#
102 lines
2.9 KiB
C#
using System.Security.Claims;
|
|
using BlueWest.Data.Auth;
|
|
using BlueWest.Data.Auth.Context.Users;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Controller = Microsoft.AspNetCore.Mvc.Controller;
|
|
|
|
namespace CodeLiturgy.Views.Controllers
|
|
{
|
|
public class AuthController : UserController
|
|
{
|
|
|
|
private readonly IAuthManager _authManager;
|
|
|
|
public AuthController(ApplicationUserManager userManager, ILogger<AuthController> logger, IAuthManager authManager) : base(userManager, logger)
|
|
{
|
|
_userManager = userManager;
|
|
_logger = logger;
|
|
_authManager = authManager;
|
|
}
|
|
|
|
public async Task<IActionResult> Index()
|
|
{
|
|
await OnEveryAction();
|
|
return View();
|
|
}
|
|
|
|
[AllowAnonymous]
|
|
[ActionName("LoginAction")]
|
|
public async Task<IActionResult> LoginAction(LoginRequest loginRequest)
|
|
{
|
|
var (identity,success) =
|
|
await _authManager.DoLogin(loginRequest);
|
|
|
|
|
|
if (!success) return Redirect(AuthLoginRoute);
|
|
|
|
if (success)
|
|
{
|
|
await HttpContext.SignInAsync(
|
|
CookieAuthenticationDefaults.AuthenticationScheme,
|
|
new ClaimsPrincipal(identity),
|
|
new AuthenticationProperties
|
|
{
|
|
IsPersistent = true,
|
|
ExpiresUtc = DateTime.UtcNow.Add(SessionConstants.DefaultSessionMaxAge)
|
|
});
|
|
|
|
HttpContext.Session.SetString("hello", "world");
|
|
|
|
return Redirect(RootLocation);
|
|
}
|
|
|
|
return Redirect(RootLocation);
|
|
}
|
|
|
|
public IActionResult Login()
|
|
{
|
|
this.HandleGlobalization();
|
|
return View();
|
|
}
|
|
|
|
public async Task<IActionResult> Account()
|
|
{
|
|
await OnEveryAction();
|
|
this.HandleGlobalization();
|
|
return View();
|
|
}
|
|
|
|
|
|
public async Task<IActionResult> Logout()
|
|
{
|
|
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
return Redirect("/");
|
|
}
|
|
|
|
|
|
|
|
public async Task<IActionResult> Signup()
|
|
{
|
|
await OnEveryAction();
|
|
return View();
|
|
}
|
|
|
|
|
|
[Microsoft.AspNetCore.Mvc.ActionName("SignupAction")]
|
|
public async Task<IActionResult> SignupAction(RegisterRequest registerRequest)
|
|
{
|
|
var result = await _authManager.CreateUserAsync(registerRequest);
|
|
|
|
if (result.Succeeded)
|
|
{
|
|
return RedirectToAction("Login");
|
|
}
|
|
|
|
return RedirectToAction("Signup");
|
|
}
|
|
|
|
}
|
|
} |