Notes/Controllers/LoginController.cs

62 lines
1.7 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;
namespace BinaryDad.Notes.Controllers
{
public class LoginController : Controller
{
2023-08-30 00:25:45 +00:00
private readonly IConfiguration configuration;
public LoginController(IConfiguration configuration)
{
this.configuration = configuration;
}
[Route("login")]
public IActionResult Login()
{
return View();
}
[ActionName(nameof(Login))]
[Route("login")]
[HttpPost]
2023-05-10 21:35:40 +00:00
public async Task<IActionResult> LoginPost([Required] string passphrase, string returnUrl)
{
if (ModelState.IsValid)
{
2023-08-30 00:25:45 +00:00
var appPassphrase = configuration["APP_PASSPHRASE"];
2023-05-10 21:35:40 +00:00
if (passphrase == appPassphrase)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.GivenName, "Ryan")
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(new ClaimsPrincipal(claimsIdentity));
return Redirect(returnUrl);
}
ModelState.AddModelError("", "Invalid login");
}
return View();
}
[Route("logout")]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync();
return Redirect("/");
}
}
}