AccountController: using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using…
- User
AccountController:
using System.Diagnostics; using Microsoft.AspNetCore.Mvc; using System.Security.Claims; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Havit.ViewModels; using System.Threading.Tasks; using Havit.Services.Interfaces;
//This is not the best way to do this, but it's the only way to deploy on github pages, so it'll do for now. public class AuthController : Controller { private readonly IUserStore _userStore; private const string SessionKeyUser = "_User";
public AuthController(IUserStore userStore) { _userStore = userStore; } public IActionResult Login() { return View(); } public IActionResult Register() { return View(); } public IActionResult AccessDenied() { return View(); } [HttpPost] public async Task<IActionResult> Login(LoginVM model) { if (ModelState.IsValid) { if (_userStore.UserExists(model.Username)) { if (_userStore.ValidateUser(model.Username, model.Password)) { var claims = new List<Claim> { new Claim(ClaimTypes.Name, model.Username), new Claim(ClaimTypes.Role, "User") }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { IsPersistent = model.RememberMe, ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1) }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); HttpContext.Session.SetString("Username", model.Username); var principal = await HttpContext.AuthenticateAsync(); if (principal.Succeeded && principal.Principal.HasClaim(c => c.Type == ClaimTypes.Role && c.Value == "User")) { Console.WriteLine("Access granted"); return RedirectToAction("Index", "HabitTracker"); } else { Console.WriteLine("Access denied"); return RedirectToAction("AccessDenied", "Auth"); } } } ModelState.AddModelError("", "Invalid username or password"); } return View(model); } [HttpPost] public async Task<IActionResult> Register(RegisterVM model) { if (ModelState.IsValid) { if (_userStore.UserExists(model.Username)) { ModelState.AddModelError("Username", "Username already exists"); return View(model); } if (!model.AgreedToTos) { ModelState.AddModelError("AgreedToTos", "You must agree to the Terms of Service"); return View(model); } if (!_userStore.AddUser(model.Username, model.Password)) { ModelState.AddModelError("", "Unable to register user. Please try again."); return View(model); } var claims = new List<Claim> { new Claim(ClaimTypes.Name, model.Username), new Claim(ClaimTypes.Role, "User") }; var claimsIdentity = new ClaimsIdentity( claims, CookieAuthenticationDefaults.AuthenticationScheme); var authProperties = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.UtcNow.AddDays(7) }; await HttpContext.SignInAsync( CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); HttpContext.Session.SetString(SessionKeyUser, model.Username); return RedirectToAction("Index", "HabitTracker"); } return View(model); } [HttpPost] public async Task<IActionResult> Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); HttpContext.Session.Clear(); return RedirectToAction("Login"); } - Assistant