/// <summary> /// this method is called when admin lohout his/her account /// </summary> /// <returns>redirect to login view</returns> public ActionResult LogOff() { db_KISDEntities _contex = new db_KISDEntities(); SystemAccessLog objSystemAccessLog = new SystemAccessLog(); var usename = Membership.GetUser().UserName; objSystemAccessLog = _contex.SystemAccessLogs.Where(x => x.UserNameTxt == usename).OrderByDescending(x => x.SystemAccessLogID).FirstOrDefault(); objSystemAccessLog.LogoutDateTime = System.DateTime.Now; _contex.Entry(objSystemAccessLog).State = System.Data.Entity.EntityState.Modified; _contex.SaveChanges(); FormsAuthentication.SignOut(); // Clear authentication cookie. HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, ""); cookie.Expires = DateTime.Now.AddYears(-1); Response.Cookies.Add(cookie); Session.Abandon(); return(RedirectToAction("Login", "Account")); }
public ActionResult Login(AccountModel accountModel, string ReturnUrl, string command) { if (string.IsNullOrEmpty(command)) { if (ModelState.IsValid) { var password = accountModel.IsCheckedRememberMe ? EncryptDecrypt.Decrypt(accountModel.Password) : accountModel.Password; password = string.IsNullOrEmpty(password) ? accountModel.Password : password; if (MembershipService.ValidateUser(accountModel.UserNameTxt, password)) { FormService.SignIn(accountModel.UserNameTxt, accountModel.RememberMe); FormsAuthentication.SetAuthCookie(accountModel.UserNameTxt, accountModel.RememberMe); var authTicket = new FormsAuthenticationTicket(1, accountModel.UserNameTxt, DateTime.Now, DateTime.Now.AddDays(30), accountModel.RememberMe, accountModel.RememberMe ? EncryptDecrypt.Encrypt(password) : "", "/"); //encrypt the ticket and add it to a cookie HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); Response.Cookies.Add(cookie); #region System Access Log db_KISDEntities _context = new db_KISDEntities(); SystemAccessLog objSystemAccessLog; #region Check for user already logged in or not objSystemAccessLog = _context.SystemAccessLogs.Where(x => x.UserNameTxt == accountModel.UserNameTxt).OrderByDescending(x => x.SystemAccessLogID).FirstOrDefault(); if (objSystemAccessLog != null) { objSystemAccessLog.LogoutDateTime = objSystemAccessLog.LogoutDateTime > System.DateTime.Now ? System.DateTime.Now : objSystemAccessLog.LogoutDateTime; _context.Entry(objSystemAccessLog).State = System.Data.Entity.EntityState.Modified; _context.SaveChanges(); } #endregion User objUser = _context.Users.Where(x => x.UserNameTxt == accountModel.UserNameTxt).FirstOrDefault(); objSystemAccessLog = new SystemAccessLog(); objSystemAccessLog.UserNameTxt = objUser.UserNameTxt; objSystemAccessLog.NameTxt = objUser.FirstNameTxt + " " + objUser.LastNameTxt; objSystemAccessLog.LoginDateTime = System.DateTime.Now; objSystemAccessLog.LogoutDateTime = Convert.ToDateTime(System.DateTime.Today.ToShortDateString() + " 23:59:00"); objSystemAccessLog.UserRoleID = _context.UserRoles.Where(x => x.UserID == objUser.UserID).FirstOrDefault().RoleID; _context.SystemAccessLogs.Add(objSystemAccessLog); _context.SaveChanges(); #endregion if (!string.IsNullOrEmpty(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/") && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\")) { return(Redirect(ReturnUrl)); } return(RedirectToAction("Index", "Home")); } ModelState.AddModelError("", "Login failed. Please check Username/Password and try again."); } return(View(accountModel)); } else { if (accountModel != null) { accountModel.Password = string.Empty; accountModel.UserNameTxt = string.Empty; accountModel.RememberMe = false; } ModelState.Clear(); return(View(accountModel)); } }