namespace mSAFETIA.Controllers
{
[NoCache]
public class AccountController : Controller
{
readonly ILogger<AccountController> _log;
public AccountController(ILogger<AccountController> log)
{
_log = log;
}
[AllowAnonymous]
public IActionResult Login()
{
if (User.Identity.IsAuthenticated)
{
return Redirect("/Mail/List");
}
else
{
return View(new LoginModel() { });
}
}
[AllowAnonymous]
[HttpPost]
public IActionResult Login(LoginModel model)
{
SAFETIAContext DB = new SAFETIAContext();
var Member = DB.Members.SingleOrDefault(t => t.로그인아이디 == model.Userid && t.비밀번호 == model.Password);
if (Member == null)
return View(model);
else
{
UserData UserData = new UserData()
{
UserID = Member.로그인아이디,
UserName = Member.이름,
};
// LoginID
List<Claim> Claims = new List<Claim>();
Claims.Add(new Claim(ClaimTypes.Name, UserData.UserName));
Claims.Add(new Claim(ClaimTypes.NameIdentifier, UserData.UserID));
// UserData
Claims.Add(new Claim(ClaimTypes.UserData, UserData.Serialize()));
var user = new ClaimsPrincipal(new ClaimsIdentity(Claims.ToArray(), CookieAuthenticationDefaults.AuthenticationScheme));
HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
user,
new AuthenticationProperties() { IsPersistent = model.Remember });
return Redirect("/Mail/List");
}
}
[Authorize]
public IActionResult Logout()
{
HttpContext.Authentication.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Login", "Account");
}
}
}