public async Task <IActionResult> Login( UserViewModel model, string returnUrl = null) { if (ModelState.IsValid) { //[!] 로그인 실패 5번 체크 if (_loginFailed.IsLoginFailed(model.UserId)) { ViewBag.IsLoginFailed = true; return(View(model)); } //if (_repository.IsCorrectUser(model.UserId, model.Password)) if (_repository.IsCorrectUser(model.UserId, Common.CryptorEngine.EncryptPassword(model.Password))) { //[!] 인증 부여 var claims = new List <Claim>() { // 로그인 아이디 지정 new Claim("UserId", model.UserId), // 기본 역할 지정, "Role" 기능에 "Users" 값 부여 new Claim(ClaimTypes.Role, "Users") // 추가 정보 기록 }; var ci = new ClaimsIdentity(claims, Common.CryptorEngine.EncryptPassword(model.Password)); await HttpContext.Authentication.SignInAsync( "Cookies", new ClaimsPrincipal(ci)); return(LocalRedirect("/User/Index")); } } return(View(model)); }
public async Task <IActionResult> Login( UserViewModel model, string returnUrl = null) { if (ModelState.IsValid) { //[!] 로그인 실패 5번 체크 if (_loginFailed.IsLoginFailed(model.UserId)) { ViewBag.IsLoginFailed = true; return(View(model)); } //if (_repository.IsCorrectUser(model.UserId, model.Password)) if (_repository.IsCorrectUser(model.UserId, (new Dul.Security.CryptorEngine()).EncryptPassword(model.Password))) { //[!] 인증 부여: 인증된 사용자의 주요 정보(Name, Role, ...)를 기록 var claims = new List <Claim>() { // 로그인 아이디 지정 new Claim("UserId", model.UserId), new Claim(ClaimTypes.NameIdentifier, model.UserId), new Claim(ClaimTypes.Name, model.UserId), //new Claim(ClaimTypes.Email, model.UserId), // // 기본 역할 지정, "Role" 기능에 "Users" 값 부여 new Claim(ClaimTypes.Role, "Users") // 추가 정보 기록 }; //var ci = new ClaimsIdentity(claims, (new Dul.Security.CryptorEngine()).EncryptPassword(model.Password)); var ci = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); //[1] 로그인 처리: Authorize 특성 사용해서 로그인 체크 가능 //[1][1] ASP.NET Core 1.X 사용: 버전업되면서 아래 메서드 사용 중지 //await HttpContext.Authentication.SignInAsync( // "Cookies", new ClaimsPrincipal(ci)); //[1][2] ASP.NET Core 2.X 사용 var authenticationProperties = new AuthenticationProperties() { ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(60), IssuedUtc = DateTimeOffset.UtcNow, IsPersistent = true }; //await HttpContext.SignInAsync("Cookies", new ClaimsPrincipal(ci), new AuthenticationProperties { IsPersistent = true }); //await HttpContext.SignInAsync("Cookies", new ClaimsPrincipal(ci), authenticationProperties); //await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(ci)); // 기본 await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme , new ClaimsPrincipal(ci), authenticationProperties); // 옵션 ////[참고] ASP.NET Core Identity에서 로그인하는 모양 //var identity = new ClaimsIdentity("Cookies"); //identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id)); //identity.AddClaim(new Claim(ClaimTypes.Name, user.UserName)); //await HttpContext.SignInAsync("Cookies", new ClaimsPrincipal(identity)); // 추가: 세션에 로그인 사용자 정보 저장 // 세션 인증 로그인 //HttpContext.Session.SetString("Username", model.UserId); return(LocalRedirect("/User/Index")); } } return(View(model)); }