2023-05-10 20:22:51 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-10 20:22:51 +00:00
|
|
|
|
[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)
|
2023-05-10 20:22:51 +00:00
|
|
|
|
{
|
|
|
|
|
if (ModelState.IsValid)
|
|
|
|
|
{
|
2023-08-30 00:25:45 +00:00
|
|
|
|
var appPassphrase = configuration["APP_PASSPHRASE"];
|
2023-05-10 20:22:51 +00:00
|
|
|
|
|
2023-05-10 21:35:40 +00:00
|
|
|
|
if (passphrase == appPassphrase)
|
2023-05-10 20:22:51 +00:00
|
|
|
|
{
|
|
|
|
|
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("/");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|