public async Task <ActionResult> LogIn(UserLogInDto userLogInDto, string returnUrl) { if (User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } var user = await accountRepository.GetUserByUserNameAndPassword(userLogInDto.UserName, userLogInDto.Password); UserLogInDetails userLogInDetails = new UserLogInDetails(); if (user != null) { //var claims = new List<Claim>(); //claims.Add(new Claim(ClaimTypes.Name, user.UserName)); //string[] roles = user.UserRoles.Split(','); //foreach (string role in roles) //{ // claims.Add(new Claim(ClaimTypes.Role, role)); //} //var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); //var principal = new ClaimsPrincipal(identity); //var props = new AuthenticationProperties(); //props.IsPersistent = false; //HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal, props).Wait(); FormsAuthentication.SetAuthCookie(userLogInDto.UserName, false); userLogInDetails.UserIP = Request.UserHostAddress.ToString(); userLogInDetails.CreatedDate = DateTime.Now; userLogInDetails.IsLogIn = true; userLogInDetails.UserID = user.UserID; accountRepository.SaveUserLogInDetails(userLogInDetails); TempData["UserID"] = user.UserID; HttpContext.Session["userid"] = user.UserID; HttpContext.Session["username"] = user.UserName; HttpContext.Session["roles"] = user.UserRoles; if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("error", "Invalid username or password"); return(View()); }
public ActionResult LogOut() { if (User.Identity.IsAuthenticated) { UserLogInDetails userLogInDetails = new UserLogInDetails(); userLogInDetails.UserIP = Request.UserHostAddress.ToString(); userLogInDetails.CreatedDate = DateTime.Now; userLogInDetails.IsLogIn = false; userLogInDetails.UserID = Convert.ToString(Session["userid"]); accountRepository.SaveUserLogInDetails(userLogInDetails); FormsAuthentication.SignOut(); Session.Abandon(); // it will clear the session at the end of request Session.Clear(); Session.RemoveAll(); HttpContext.Session.Clear(); } return(RedirectToAction("LogIn")); }
public void SaveUserLogInDetails(UserLogInDetails userLogInDetails) { var result = appDbContext.UserLogInDetails.Add(userLogInDetails); appDbContext.SaveChanges(); }