55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
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
|
|
{
|
|
[Route("login")]
|
|
public IActionResult Login()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[ActionName(nameof(Login))]
|
|
[Route("login")]
|
|
[HttpPost]
|
|
public async Task<IActionResult> LoginPost([Required] string passPhrase, string returnUrl)
|
|
{
|
|
if (ModelState.IsValid)
|
|
{
|
|
var appPassPhrase = Environment.GetEnvironmentVariable("APP_PASS_PHRASE");
|
|
|
|
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("/");
|
|
}
|
|
}
|
|
}
|