public ActionResult LogOff() { try { Dictionary <int, CheckSessionData> dictionary = CheckSessionData.GetSessionValues(); if (dictionary[3].Id != null || dictionary[3].Id != "") { int userId = Convert.ToInt32(dictionary[3].Id); _loginStatusFactory = new LoginStatusFactory(); SEC_LoginStatus loginStatus = _loginStatusFactory.FindBy(x => x.UserID == userId).FirstOrDefault(); loginStatus.PresentLogInStatus = false; loginStatus.LogOutTime = DateTime.Now; loginStatus.ForcedLogOutStatus = false; _loginStatusFactory.Edit(loginStatus); _loginStatusFactory.Save(); System.Web.HttpContext.Current.Session["LoginUserID"] = 0; System.Web.HttpContext.Current.Session["LoginUserName"] = 0; System.Web.HttpContext.Current.Session["LoginEmployee"] = 0; System.Web.HttpContext.Current.Session["LoginCompanyID"] = 0; System.Web.HttpContext.Current.Session["LoginBranchID"] = 0; System.Web.HttpContext.Current.Session["LoginUserFullName"] = 0; System.Web.HttpContext.Current.Session["UserGroupID"] = 0; System.Web.HttpContext.Current.Session["IPAddress"] = 0; Session["logInSession"] = null; return(Redirect("/#!/")); } return(Redirect("/#!/")); } catch (Exception) { return(Redirect("/#!/")); } }
public ActionResult Login(LogOnModel model) { try { if (ModelState.IsValid) { bool getLan = false; string visitorIpAddress = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (String.IsNullOrEmpty(visitorIpAddress)) { visitorIpAddress = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; } if (string.IsNullOrEmpty(visitorIpAddress)) { visitorIpAddress = System.Web.HttpContext.Current.Request.UserHostAddress; } if (string.IsNullOrEmpty(visitorIpAddress) || visitorIpAddress.Trim() == "::1") { getLan = true; visitorIpAddress = string.Empty; } if (getLan && string.IsNullOrEmpty(visitorIpAddress)) { //This is for Local(LAN) Connected ID Address string stringHostName = Dns.GetHostName(); //Get Ip Host Entry IPHostEntry ipHostEntries = Dns.GetHostEntry(stringHostName); ipHostEntries = System.Net.Dns.GetHostEntry(Request.ServerVariables["REMOTE_HOST"]); //Get Ip Address From The Ip Host Entry Address List IPAddress[] arrIpAddress = ipHostEntries.AddressList; try { visitorIpAddress = arrIpAddress[arrIpAddress.Length - 2].ToString(); } catch { try { visitorIpAddress = arrIpAddress[0].ToString(); } catch { try { arrIpAddress = Dns.GetHostAddresses(stringHostName); visitorIpAddress = arrIpAddress[0].ToString(); } catch { visitorIpAddress = "127.0.0.1"; } } } } //////////////////////////////////// _securityFactory = new SecurityFactorys(); _userInformationFactory = new UserFactory(); _employeeFactory = new EmployeeFactory(); model.UserName = model.UserName.ToLower().Trim(); var logInStatus = _securityFactory.CheckLogIn(new LogOnModel { CompanyID = model.CompanyID, BranchID = model.BranchID, UserName = model.UserName, Password = model.Password }); if (logInStatus.IsAllowed) { var aSecurityUser = _userInformationFactory.FindBy(x => x.UserName.Contains(model.UserName)).FirstOrDefault(); var aCompanyUser = _employeeFactory.FindBy(x => x.EmployeeID == aSecurityUser.EmployeeID).FirstOrDefault(); if (aSecurityUser != null) { System.Web.HttpContext.Current.Session["LoginEmployee"] = aSecurityUser.EmployeeID; System.Web.HttpContext.Current.Session["LoginCompanyID"] = aCompanyUser.CompanyID; System.Web.HttpContext.Current.Session["LoginBranchID"] = aCompanyUser.BranchID; System.Web.HttpContext.Current.Session["LoginUserID"] = aSecurityUser.ID; System.Web.HttpContext.Current.Session["LoginUserName"] = aSecurityUser.UserName; System.Web.HttpContext.Current.Session["LoginUserFullName"] = aSecurityUser.Employee.EmpName; System.Web.HttpContext.Current.Session["UserGroupID"] = aSecurityUser.UserGroupID; System.Web.HttpContext.Current.Session["IPAddress"] = visitorIpAddress; System.Web.HttpContext.Current.Session["LoginPhoto"] = GetLoginPhoto(aSecurityUser.EmployeeID); string[] computerName = null; //try //{ // computerName = Dns.GetHostEntry(Request.ServerVariables["REMOTE_ADDR"]).HostName.Split(new Char[] { '.' }); //} //catch (Exception) //{ //} if (computerName != null) { System.Web.HttpContext.Current.Session["PCName"] = computerName[0]; } else { System.Web.HttpContext.Current.Session["PCName"] = "N/A"; } if (!String.IsNullOrEmpty(model.UserName)) { if (!aSecurityUser.UserName.Equals(model.UserName, StringComparison.Ordinal)) { return(Json(new { success = false, message = "Incorrect User Name or Password." }, JsonRequestBehavior.AllowGet)); } } else { System.Web.HttpContext.Current.Session["LoginUserID"] = 0; } if (!logInStatus.IsAllowed) { return(Json(new { success = false, message = logInStatus.Message }, JsonRequestBehavior.AllowGet)); } //if (String.IsNullOrEmpty(model.UserName)) throw new ArgumentException("Value cannot be null or empty.", "userName"); //FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); SEC_LoginStatus tblLogInStatus = new SEC_LoginStatus(); _loginStatusFactory = new LoginStatusFactory(); tblLogInStatus.UserID = aSecurityUser.ID; tblLogInStatus.PresentLogInStatus = true; tblLogInStatus.LogInTime = DateTime.Now; tblLogInStatus.LogOutTime = DateTime.Now; tblLogInStatus.ForcedLogOutStatus = false; _loginStatusFactory.Add(tblLogInStatus); _loginStatusFactory.Save(); Session["logInSession"] = "true"; return(Json(new { success = true, message = "Success" }, JsonRequestBehavior.AllowGet)); // } return(Json(new { success = false, message = "The user name or password provided is incorrect." }, JsonRequestBehavior.AllowGet)); } return(Json(new { success = false, message = logInStatus.Message }, JsonRequestBehavior.AllowGet)); } return(Json(new { success = false, message = "The user name or password provided is incorrect." }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { //Route(); return(Json(new { success = false, message = e.Message }, JsonRequestBehavior.AllowGet)); } //return Json(new { success = false, message = "The user name or password provided is incorrect. 4" }, JsonRequestBehavior.AllowGet); }
//public SecurityFactorys(ICBSEntities context) //{ // this.context = context; //} public LogInStatus CheckLogIn(LogOnModel entity) { LogInStatus _LogInStatus = new LogInStatus(); Dictionary <string, string> list = new Dictionary <string, string>(); Encription encription = new Encription(); try { _loginStatusFactory = new LoginStatusFactory(); _userFactory = new UserFactory(); //var data = _userFactory.GetAll().ToList(); //TBLA_USER_INFORMATION tblUserInformation = _userFactory.FindBy(x => x.UserName == entity.UserName && x.IsActive == true && x.TBLB_COMPANY.Code.ToLower() == entity.Company.ToLower()).FirstOrDefault(); SEC_UserInformation tblUserInformation = _userFactory.FindBy(x => x.UserName == entity.UserName && x.CompanyID == entity.CompanyID && x.BranchID == entity.BranchID && x.IsActive == true).FirstOrDefault(); if (tblUserInformation != null) { SEC_LoginStatus logInStatus = _loginStatusFactory.FindBy(x => x.UserID == tblUserInformation.ID).FirstOrDefault(); if (logInStatus != null) { if (logInStatus.ForcedLogOutStatus == true) { _LogInStatus.IsAllowed = false; _LogInStatus.Message = "The Page is Under maintenance"; } else { _userPasswordFactory = new UserPasswordFactory(); SEC_Password tblPassword = _userPasswordFactory.FindBy(x => x.ID == tblUserInformation.PasswordID).FirstOrDefault(); if (tblPassword != null && encription.Decrypt(tblPassword.NewPassword).Trim() == (entity.Password.Trim())) { { list.Add("UserId", tblUserInformation.ID.ToString()); list.Add("UserName", tblUserInformation.UserName); list.Add("Name", tblUserInformation.UserFullName); list.Add("UserEmployee", tblUserInformation.EmployeeID.ToString()); list.Add("UserCompany", tblUserInformation.CompanyID.ToString()); list.Add("UserBranch", tblUserInformation.BranchID.ToString()); _LogInStatus.IsAllowed = true; _LogInStatus.Status = list; _LogInStatus.Message = "Login Successfully"; } } else { _LogInStatus.IsAllowed = false; _LogInStatus.Message = "Password or User Name does not match"; } } } else { _userPasswordFactory = new UserPasswordFactory(); SEC_Password tblPassword = _userPasswordFactory.FindBy(x => x.ID == tblUserInformation.PasswordID).FirstOrDefault(); if (tblPassword != null && encription.Decrypt(tblPassword.NewPassword).Trim() == (entity.Password.Trim())) { { list.Add("UserId", tblUserInformation.ID.ToString()); list.Add("UserName", tblUserInformation.UserName); list.Add("Name", tblUserInformation.UserFullName); list.Add("UserEmployee", tblUserInformation.EmployeeID.ToString()); list.Add("UserCompany", tblUserInformation.CompanyID.ToString()); list.Add("UserBranch", tblUserInformation.BranchID.ToString()); _LogInStatus.IsAllowed = true; _LogInStatus.Status = list; _LogInStatus.Message = "Login Successfully"; } } else { _LogInStatus.IsAllowed = false; _LogInStatus.Message = "Password or User Name not matching"; } } } else { _LogInStatus.IsAllowed = false; _LogInStatus.Message = "User are not exist"; } return(_LogInStatus); } catch (Exception ex) { throw ex; } }