/// <summary> /// Get the password given an email (or username) /// </summary> /// <param name="sEmail"></param> /// <returns></returns> private static string GetUserPassword(string sEmail) { string sRetVal = ""; try { string sSql = "SELECT Password FROM Users Where Email = @Email"; SqlParameter paramEmail = new SqlParameter("@Email", SqlDbType.VarChar); paramEmail.Value = sEmail; SqlDataReader dt = GetDataReaderWithParams(sSql, paramEmail); if (dt.Read()) { XCryptEngine MyScriptEngine = null; MyScriptEngine = new XCryptEngine(); MyScriptEngine.Algorithm = XCryptEngine.AlgorithmType.DES; MyScriptEngine.InitializeEngine(); sRetVal = MyScriptEngine.Decrypt(dt["Password"].ToString());// After Script Engine Init MyScriptEngine.DestroyEngine(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); } return(sRetVal); }
public ActionResult CheckAccount(string txtUserName, string txtPassword, string rm) { if (ModelState.IsValid) { AccountModel accountModel = new AccountModel(); string passXC = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(txtPassword, "pl"); bool check = accountModel.CheckAccount(new AccountEntity(txtUserName, passXC)); if (check) { Session["username"] = txtUserName; if (rm != null && rm.Equals("on")) { HttpCookie remme = new HttpCookie("remme"); remme["username"] = txtUserName; remme["password"] = txtPassword; remme.Expires = DateTime.Now.AddDays(365); HttpContext.Response.Cookies.Add(remme); } FormsAuthentication.SetAuthCookie(txtUserName, false); return(View("~/Views/Home/Index.cshtml")); } else { ViewBag.error = "Error Password and User"; return(View("~/Views/Home/Index.cshtml")); } } else { ViewBag.error = "Error"; return(View("~/Views/LoginAccount/LoginForm.cshtml")); } }
public ActionResult AddRegistration(AccountEntity account) { if (ModelState.IsValid) { AccountModel accountModel = new AccountModel(); string passXC = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(account.AccountPass, "pl"); account.AccountPass = passXC; bool check = accountModel.AddAccount(account); if (check == true) { return(View("~/Views/LoginAccount/LoginForm.cshtml")); } else { ViewBag.error = "Error"; ListRole(); return(View("~/Views/LoginAccount/RegistrationForm.cshtml")); } } else { ListRole(); ViewBag.error = "Error"; return(View("~/Views/LoginAccount/RegistrationForm.cshtml")); } }
public ActionResult ChangePassword(string oldPassword, string newPassword, string repeatNewPassword) { int CurrentUser = (int)Session["UserID"]; if (oldPassword == null || oldPassword == "" || newPassword == null || newPassword == "" || repeatNewPassword == null || repeatNewPassword == "") { return(View()); } string oldPassEncrypt = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(oldPassword.Trim(), "pl"); string newPassEncrypt = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(newPassword.Trim(), "pl"); if (newPassword != repeatNewPassword) { ViewData["Notification"] = "Mật khẩu không khớp."; return(View()); } else { if (db.SYS_USER.Where(a => a.ID == CurrentUser).Where(a => a.Password == oldPassEncrypt).Count() > 0) { SYS_USER sysUser = db.SYS_USER.Single(a => a.ID == CurrentUser); sysUser.Password = newPassEncrypt; db.SaveChanges(); ViewData["NotificationSuccess"] = "Thay đổi mật khẩu thành công!"; return(View()); } else { ViewData["Notification"] = "Mật khẩu cũ không đúng."; return(View()); } } }
public JsonResult ResetPassword(string oldPass, string newPass, string rePass) { try { int id = Convert.ToInt32(Session["account_id"]); string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(oldPass, "pl"); string rePasss = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(rePass, "pl"); var user = db.Accounts.Where(x => x.ID == id).FirstOrDefault(); if (string.IsNullOrEmpty(oldPass) || string.IsNullOrEmpty(newPass) || string.IsNullOrEmpty(rePass)) { return(Json(new Result() { CodeError = 1, Data = "Mật khẩu không được để trống" }, JsonRequestBehavior.AllowGet)); } if (!newPass.Equals(rePass)) { return(Json(new Result() { CodeError = 1, Data = "2 mật khẩu không trùng khớp" }, JsonRequestBehavior.AllowGet)); } if (!user.Password.Equals(passXc)) { return(Json(new Result() { CodeError = 1, Data = "Mật khẩu cũ không đúng" }, JsonRequestBehavior.AllowGet)); } user.Password = rePasss; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return(Json(new Result() { CodeError = 2, Data = "Thay đổi mật khẩu thành công" }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { return(Json(new Result() { CodeError = 1, Data = "Có lỗi xảy ra. Vui lòng thử lại!" }, JsonRequestBehavior.AllowGet)); } }
public ActionResult LibrarianLogin(string username, string password) { string passEncrypt = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(password, "pl"); var librarianAccount = db.SP_SYS_USER_LOGIN(username, passEncrypt).FirstOrDefault(); if (librarianAccount == null) { ViewData["LibraryNotification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } var librarianInfo = librarianAccount.Name; Session["LibrarianName"] = librarianInfo; return(RedirectToAction("CreateNotification", "Notification")); }
public JsonResult ResetPassword(string UserID) { try { string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt("123456", "pl"); int id = int.Parse(UserID); var Acc = db.Accounts.Where(x => x.ID == id).SingleOrDefault(); Acc.Password = passXc; db.Entry(Acc).State = EntityState.Modified; db.SaveChanges(); return(Json("Reset mật khẩu thành công", JsonRequestBehavior.AllowGet)); } catch (Exception) { return(Json("Có lỗi xảy ra !!! Vui lòng thử lại.", JsonRequestBehavior.AllowGet)); } }
public ActionResult Index(string username, string password, string rm) { string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(password, "pl"); var checkuser = db.Accounts.Where(x => x.Username == username).Where(y => y.Password == passXc).ToList(); if (checkuser.Count > 0) { Session["UserID"] = checkuser[0].ID; int id = checkuser[0].ID; var Name = db.Accounts.Where(x => x.ID == id).FirstOrDefault <Account>(); Session["Name"] = Name.Name; Session["username"] = Name.Username; Session["Position"] = Name.Position; Session["isAdmin"] = Name.ADMIN; GetPermission(id); if (!String.IsNullOrEmpty(rm)) { if (rm.Equals("on")) { HttpCookie remme = new HttpCookie("remme"); remme["username"] = Name.Username; remme["password"] = password; remme.Expires = DateTime.Now.AddDays(365); HttpContext.Response.Cookies.Add(remme); } } if (Name.ADMIN) { return(RedirectToAction("Index", "ManagementUser")); } string url = (string)Session["url"]; if (url == null) { ViewData["Notification"] = "Bạn không có bất kì quyền hạn nào."; Session.Abandon(); return(View()); } return(Redirect(url)); } else { ViewData["Notification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } }
public ActionResult Index(string username, string password) { string passEncrypt = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(password, "pl"); var checkUser = db.SP_SYS_USER_LOGIN(username, passEncrypt).ToList(); if (checkUser.Count > 0) { int UserID = checkUser[0].ID; Session["UserID"] = UserID; GetPermission(UserID); return(RedirectToAction("Index", "Home")); } else { ViewData["Notification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } }
public bool Test() { string teststring = "I am a test string"; //byte[] m_myArr; m_myEncrypter = new XCryptEngine(XCryptEngine.AlgorithmType.BlowFish); try{ m_myWriter = new StreamWriter("bgame.conf"); for (int w = 0; w < 10; w++) { m_myWriter.WriteLine(m_myEncrypter.Encrypt(teststring, "bdbgame")); } } catch (Exception e) { new genericDialogs().ShowErrorBox(e.ToString()); return(false); } return(true); }
public JsonResult UpdateUser(int ID, string Name, string Username, string Position, string Password, string RepeatPassword, string NVID, int module1, int module2, int module3, int module4, int module5, int module6, int module7, int module8, int module9, int module11, int module12, int module13, int module14, int module15, int module17, int module18, int module19, int module20, string rights) { if (db.Accounts.Where(x => x.Username == Username).Where(y => y.ID != ID).Count() > 0) { return(Json(new Result() { CodeError = 2, Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!" }, JsonRequestBehavior.AllowGet)); } if (!String.IsNullOrEmpty(NVID)) { var nv = db.Employees.Where(x => x.employee_id.Equals(NVID)).FirstOrDefault(); if (nv == null) { return(Json(new Result() { CodeError = 2, Data = "Mã nhân viên <strong style='color:black; '>" + NVID + "</strong> không tồn tại!" }, JsonRequestBehavior.AllowGet)); } } else { NVID = null; } string InvalidFields = ""; if (String.IsNullOrEmpty(Name)) { InvalidFields += "Họ Tên -"; } if (String.IsNullOrEmpty(Username)) { InvalidFields += " Tên đăng nhập -"; } if (String.IsNullOrEmpty(Position)) { InvalidFields += " Chức vụ -"; } if (InvalidFields != "") { return(Json(new Result() { CodeError = 1, Data = InvalidFields.Substring(0, InvalidFields.Length) + " không thể để trống!" }, JsonRequestBehavior.AllowGet)); } if (Password != RepeatPassword) { return(Json(new Result() { CodeError = 1, Data = "Mật khẩu không khớp!" }, JsonRequestBehavior.AllowGet)); } var user = db.Accounts.SingleOrDefault(x => x.ID == ID); using (DbContextTransaction trans = db.Database.BeginTransaction()) { try { var rightsSplit = rights.Split(','); var rightRemove = db.Account_Right_Detail.Where(x => x.AccountID == ID).ToList(); foreach (var r in rightRemove) { db.Account_Right_Detail.Remove(r); } db.SaveChanges(); foreach (var r in rightsSplit) { if (!String.IsNullOrEmpty(r)) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = int.Parse(r) }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); updateModule(module1, ID, user.CDVT, 1); updateModule(module2, ID, user.TCLD, 2); updateModule(module3, ID, user.KCS, 3); updateModule(module4, ID, user.DK, 4); updateModule(module5, ID, user.BGD, 5); updateModule(module6, ID, user.PXKT, 6); updateModule(module8, ID, user.PXDL, 8); updateModule(module9, ID, user.PXVT, 9); updateModule(module11, ID, user.PXPV, 11); updateModule(module12, ID, user.PXDS, 12); updateModule(module13, ID, user.PXCDM, 13); updateModule(module14, ID, user.PXTGQLM, 14); updateModule(module15, ID, user.PXXD, 15); updateModule(module17, ID, user.AT, 17); updateModule(module18, ID, user.KCM, 18); updateModule(module19, ID, Convert.ToBoolean(user.PXCKSC), 19); updateModule(module20, ID, user.PXCBT, 20); if (Convert.ToBoolean(module7).Equals(user.ADMIN)) { } else { var listRight = db.Account_Right.ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("Account.GetFunctionsByUID {0}", ID).ToList(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module7 == 0) { module1 = 0; module2 = 0; module3 = 0; module4 = 0; module5 = 0; module6 = 0; module7 = 0; module8 = 0; module9 = 0; module11 = 0; module12 = 0; module13 = 0; module14 = 0; module15 = 0; module17 = 0; module18 = 0; module19 = 0; module20 = 0; user.Role = 3; } else { module7 = 1; user.Role = 2; db.SaveChanges(); } } user.Name = Name; user.Username = Username; if (String.IsNullOrEmpty(Password)) { } else { string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl"); user.Password = passXc; } user.Position = Position; user.NVID = NVID; user.CDVT = Convert.ToBoolean(module1); user.TCLD = Convert.ToBoolean(module2); user.KCS = Convert.ToBoolean(module3); user.DK = Convert.ToBoolean(module4); user.BGD = Convert.ToBoolean(module5); user.PXKT = Convert.ToBoolean(module6); user.ADMIN = Convert.ToBoolean(module7); user.PXDL = Convert.ToBoolean(module8); user.PXVT = Convert.ToBoolean(module9); user.PXPV = Convert.ToBoolean(module11); user.PXDS = Convert.ToBoolean(module12); user.PXCDM = Convert.ToBoolean(module13); user.PXTGQLM = Convert.ToBoolean(module14); user.PXXD = Convert.ToBoolean(module15); user.AT = Convert.ToBoolean(module17); user.PXCKSC = Convert.ToBoolean(module19); user.KCM = Convert.ToBoolean(module18); user.PXCBT = Convert.ToBoolean(module20); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); trans.Commit(); } catch (Exception e) { trans.Rollback(); return(Json(new Result() { CodeError = 2, Data = "Có lỗi vui lòng kiểm tra lại!" }, JsonRequestBehavior.AllowGet)); } } return(Json(new Result() { CodeError = 0, Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được cập nhật thành công cho <strong style='color:black;'>" + Name + "</strong>" }, JsonRequestBehavior.AllowGet)); }
public JsonResult AddNewUser(string Name, string Username, string Position, string Password, string RepeatPassword, string NVID, int module1, int module2, int module3, int module4, int module5, int module6, int module7, int module8, int module9, int module11, int module12, int module13, int module14, int module15, int module17, int module18, int module19, int module20, string rights) { if (db.Accounts.Where(x => x.Username == Username).Count() > 0) { return(Json(new Result() { CodeError = 2, Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!" }, JsonRequestBehavior.AllowGet)); } string InvalidFields = ""; if (!String.IsNullOrEmpty(NVID)) { if (db.Employees.Where(x => x.employee_id == NVID).Count() == 0) { return(Json(new Result() { CodeError = 2, Data = "Mã nhân viên <strong style='color:black; '>" + NVID + "</strong> không tồn tại!" }, JsonRequestBehavior.AllowGet)); } } else { InvalidFields += " Mã nhân viên -"; } if (String.IsNullOrEmpty(Name)) { InvalidFields += " Họ Tên -"; } if (String.IsNullOrEmpty(Username)) { InvalidFields += " Tên đăng nhập -"; } if (String.IsNullOrEmpty(Position)) { InvalidFields += " Chức vụ -"; } if (InvalidFields != "") { return(Json(new Result() { CodeError = 1, Data = InvalidFields.Substring(0, InvalidFields.Length) + " không được để trống !!!" }, JsonRequestBehavior.AllowGet)); } if (String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(RepeatPassword)) { return(Json(new Result() { CodeError = 1, Data = "Mật khẩu không được để trống !!!" }, JsonRequestBehavior.AllowGet)); } else { if (Password != RepeatPassword) { return(Json(new Result() { CodeError = 1, Data = "Mật khẩu nhập lại không khớp !!!" }, JsonRequestBehavior.AllowGet)); } } if (InvalidFields != "") { return(Json(new Result() { CodeError = 1, Data = InvalidFields }, JsonRequestBehavior.AllowGet)); } else { string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl"); using (DbContextTransaction trans = db.Database.BeginTransaction()) { try { Account a = new Account() { Name = Name, Username = Username, Password = passXc, Position = Position, NVID = NVID, Role = 3, CDVT = Convert.ToBoolean(module1), TCLD = Convert.ToBoolean(module2), KCS = Convert.ToBoolean(module3), DK = Convert.ToBoolean(module4), BGD = Convert.ToBoolean(module5), PXKT = Convert.ToBoolean(module6), PXDL = Convert.ToBoolean(module8), PXVT = Convert.ToBoolean(module9), PXPV = Convert.ToBoolean(module11), PXDS = Convert.ToBoolean(module12), PXCDM = Convert.ToBoolean(module13), PXTGQLM = Convert.ToBoolean(module14), PXXD = Convert.ToBoolean(module15), AT = Convert.ToBoolean(module17), PXCKSC = Convert.ToBoolean(module19), KCM = Convert.ToBoolean(module18), PXCBT = Convert.ToBoolean(module20) }; db.Accounts.Add(a); db.SaveChanges(); var acc = db.Accounts.Where(x => x.Username == Username).FirstOrDefault(); var rightsSplit = rights.Split(','); foreach (var r in rightsSplit) { if (!String.IsNullOrEmpty(r)) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = int.Parse(r) }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); addModule(module1, acc.ID, 1); addModule(module2, acc.ID, 2); addModule(module3, acc.ID, 3); addModule(module4, acc.ID, 4); addModule(module5, acc.ID, 5); addModule(module6, acc.ID, 6); addModule(module8, acc.ID, 8); addModule(module9, acc.ID, 9); addModule(module11, acc.ID, 11); addModule(module12, acc.ID, 12); addModule(module13, acc.ID, 13); addModule(module14, acc.ID, 14); addModule(module15, acc.ID, 15); addModule(module17, acc.ID, 17); addModule(module18, acc.ID, 18); addModule(module19, acc.ID, 19); addModule(module20, acc.ID, 20); if (module7 == 1) { var user = db.Accounts.SingleOrDefault(x => x.ID == acc.ID); user.Name = Name; user.Username = Username; user.Password = passXc; user.Position = Position; user.Role = 2; user.NVID = NVID; user.CDVT = false; user.TCLD = false; user.KCS = false; user.DK = false; user.BGD = false; user.PXKT = false; user.ADMIN = true; user.PXDL = false; user.PXVT = false; user.PXPV = false; user.PXDS = false; user.PXCDM = false; user.PXTGQLM = false; user.PXXD = false; user.AT = false; user.KCM = false; user.PXCKSC = false; user.PXCBT = false; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } trans.Commit(); return(Json(new Result() { CodeError = 0, Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được thêm mới thành công cho <strong style='color:black;'>" + Name + "</strong>" }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { trans.Rollback(); return(Json(new Result() { CodeError = 2, Data = "Có lỗi vui lòng kiểm tra lại!" }, JsonRequestBehavior.AllowGet)); } } } }
public ActionResult Index(string username, string password, string rm) { //try //{ if (password == null) { return(RedirectToAction("Index")); } string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(password, "pl"); var checkuser = db.Accounts.Where(x => x.Username == username).Where(y => y.Password == passXc).SingleOrDefault(); if (checkuser != null) { if (checkuser.Username.Equals(username) && checkuser.Password.Equals(passXc)) { Session["UserID"] = checkuser.ID; Session["time"] = DateTime.Now; int id = checkuser.ID; var Name = db.Database.SqlQuery <GetAccountInfo_Result>("Account.GetEmployeeInfoByAccountID {0}", id).FirstOrDefault(); Session["departName"] = Name.department_name.Trim(); Session["departID"] = Name.department_id.Trim(); Session["account_id"] = Name.ID; Session["Name"] = Name.BASIC_INFO_full_name; Session["username"] = Name.Username.Trim(); Session["Position"] = Name.Position.Trim(); Session["isAdmin"] = Name.ADMIN; Session["Role"] = Name.Role; GetPermission(id); //string hashtoken = Hash.Encrypt.EncryptString(password,"quanghanhcoals"); //if (!String.IsNullOrEmpty(rm)) //{ // if (rm.Equals("on")) // { // HttpCookie remme = new HttpCookie("token"); // remme["token"] = hashtoken; // remme["uid"] = Name.ID.ToString(); // remme.Expires = DateTime.Now.AddDays(365); // remme.Secure = true; // remme.HttpOnly = true; // HttpContext.Response.Cookies.Add(remme); // checkuser.token = hashtoken; // try // { // db.Entry(checkuser).State = EntityState.Modified; // db.SaveChanges(); // } // catch (Exception e) { } // } //} if (Name.ADMIN) { return(RedirectToAction("Index", "ManagementUser")); } string url = (string)Session["url"]; if (url == null) { ViewData["Notification"] = "Tài khoản chưa được kích hoạt"; Session.Abandon(); return(View()); } return(Redirect(url)); } else { ViewData["Notification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } } else { ViewData["Notification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } //} //catch (Exception e) //{ // ViewData["Notification"] = "Có lỗi xảy ra. Vui lòng thử lại!"; // return View(); //} }
public ActionResult Index(string username, string password, string rm) { if (password == null) { return(RedirectToAction("Index")); } string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(password, "pl"); var checkuser = db.Accounts.Where(x => x.Username == username).Where(y => y.Password == passXc).SingleOrDefault(); if (checkuser != null) { if (checkuser.Username.Equals(username) && checkuser.Password.Equals(passXc)) { Session["UserID"] = checkuser.ID; Session["time"] = DateTime.Now; int id = checkuser.ID; var Name = db.Database.SqlQuery <InfoAccount>(@"select a.ID, ep.BASIC_INFO_full_name, a.Username, a.Position, a.ADMIN, d.department_name, d.department_id, a.Role from Account.Account a, HumanResources.Employee ep , General.Department d where a.NVID = ep.employee_id and d.department_id = ep.current_department_id and a.ID = @id", new SqlParameter("id", id)).FirstOrDefault(); Session["departName"] = Name.department_name.Trim(); Session["departID"] = Name.department_id.Trim(); Session["account_id"] = Name.ID; Session["Name"] = Name.Ten; Session["username"] = Name.Username.Trim(); Session["Position"] = Name.Position.Trim(); Session["isAdmin"] = Name.ADMIN; Session["Role"] = Name.Role; GetPermission(id); //thư viện đang dùng cho hashpass không decrypt được nên phải dùng thư viện khác để set pass cookie string hashtoken = Hash.Encrypt.EncryptString(password, "quanghanhcoals"); if (!String.IsNullOrEmpty(rm)) { if (rm.Equals("on")) { HttpCookie remme = new HttpCookie("token"); remme["token"] = hashtoken; remme["uid"] = Name.ID.ToString(); remme.Expires = DateTime.Now.AddDays(365); remme.Secure = true; remme.HttpOnly = true; HttpContext.Response.Cookies.Add(remme); checkuser.token = hashtoken; try { db.Entry(checkuser).State = EntityState.Modified; db.SaveChanges(); } catch (Exception e) { } } } if (Name.ADMIN) { return(RedirectToAction("Index", "ManagementUser")); } string url = (string)Session["url"]; if (url == null) { ViewData["Notification"] = "Tài khoản chưa được kích hoạt"; Session.Abandon(); return(View()); } return(Redirect(url)); } else { ViewData["Notification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } } else { ViewData["Notification"] = "Tên đăng nhập/mật khẩu không đúng!"; return(View()); } }
public JsonResult AddNewUser(string Name, string Username, string Position, string Password, string RepeatPassword, int module1, int module2, int module3, int module4, int module5, int module6, int module7, string rights) { if (db.Accounts.Where(x => x.Username == Username).Count() > 0) { return(Json(new Result() { CodeError = 2, Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!" }, JsonRequestBehavior.AllowGet)); } string InvalidFields = ""; if (String.IsNullOrEmpty(Name)) { InvalidFields += "Họ Tên -"; } if (String.IsNullOrEmpty(Username)) { InvalidFields += "Tên đăng nhập -"; } if (String.IsNullOrEmpty(Position)) { InvalidFields += "Chức vụ -"; } if (InvalidFields != "") { InvalidFields += " không thể để trống !!!"; } if (String.IsNullOrEmpty(Password) || String.IsNullOrEmpty(RepeatPassword)) { return(Json(new Result() { CodeError = 1, Data = "Mật khẩu không được để trống !!!" }, JsonRequestBehavior.AllowGet)); } else { if (Password != RepeatPassword) { InvalidFields += "<br />Mật khảu không khớp !!!"; } } if (InvalidFields != "") { return(Json(new Result() { CodeError = 1, Data = InvalidFields }, JsonRequestBehavior.AllowGet)); } else { //var listRightBasic = db.Database.SqlQuery<rightBasic>("select a.ID from Account_Right a where a.isBasic = '1' and a.ModuleID='1'").ToList<rightBasic>(); string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl"); using (DbContextTransaction trans = db.Database.BeginTransaction()) { try { Account a = new Account() { Name = Name, Username = Username, Password = passXc, Position = Position, CDVT = Convert.ToBoolean(module1), TCLD = Convert.ToBoolean(module2), KCS = Convert.ToBoolean(module3), DK = Convert.ToBoolean(module4), BGD = Convert.ToBoolean(module5), PXKT = Convert.ToBoolean(module6) }; db.Accounts.Add(a); db.SaveChanges(); var acc = db.Accounts.Where(x => x.Username == Username).FirstOrDefault(); var rightsSplit = rights.Split(','); foreach (var r in rightsSplit) { if (!String.IsNullOrEmpty(r)) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = int.Parse(r) }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); if (module1 == 1) { var listRight = db.Account_Right.Where(x => x.ModuleID == 1 + "").ToList(); var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='1'").ToList <Account_Right_Detail>(); foreach (var r in rightRemove) { var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } if (module2 == 1) { var listRight = db.Account_Right.Where(x => x.ModuleID == 2 + "").ToList(); var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='2'").ToList <Account_Right_Detail>(); foreach (var r in rightRemove) { var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } if (module3 == 1) { var listRight = db.Account_Right.Where(x => x.ModuleID == 3 + "").ToList(); var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='3'").ToList <Account_Right_Detail>(); foreach (var r in rightRemove) { var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } if (module4 == 1) { var listRight = db.Account_Right.Where(x => x.ModuleID == 4 + "").ToList(); var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='4'").ToList <Account_Right_Detail>(); foreach (var r in rightRemove) { var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } if (module5 == 1) { var listRight = db.Account_Right.Where(x => x.ModuleID == 5 + "").ToList(); var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='5'").ToList <Account_Right_Detail>(); foreach (var r in rightRemove) { var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } if (module6 == 1) { var listRight = db.Account_Right.Where(x => x.ModuleID == 6 + "").ToList(); var rightRemove = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + acc.ID + "' and a.ModuleID='6'").ToList <Account_Right_Detail>(); foreach (var r in rightRemove) { var del = db.Account_Right_Detail.Where(x => x.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } if (module7 == 1) { var listRight = db.Account_Right.ToList(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = acc.ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } var user = db.Accounts.SingleOrDefault(x => x.ID == acc.ID); user.Name = Name; user.Username = Username; user.Password = passXc; user.Position = Position; user.CDVT = true; user.TCLD = true; user.KCS = true; user.DK = true; user.BGD = true; user.PXKT = true; user.ADMIN = true; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } trans.Commit(); return(Json(new Result() { CodeError = 0, Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được thêm mới thành công cho <strong style='color:black;'>" + Name + "</strong>" }, JsonRequestBehavior.AllowGet)); } catch (Exception) { trans.Rollback(); return(Json(new Result() { CodeError = 2, Data = "Có lỗi vui lòng kiểm tra lại!" }, JsonRequestBehavior.AllowGet)); } } } }
public JsonResult UpdateUser(int ID, string Name, string Username, string Position, string Password, string RepeatPassword, int module1, int module2, int module3, int module4, int module5, int module6, int module7, string rights) { if (db.Accounts.Where(x => x.Username == Username).Where(y => y.ID != ID).Count() > 0) { return(Json(new Result() { CodeError = 2, Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!" }, JsonRequestBehavior.AllowGet)); } string InvalidFields = ""; if (String.IsNullOrEmpty(Name)) { InvalidFields += "Họ Tên-"; } if (String.IsNullOrEmpty(Username)) { InvalidFields += "Tên đăng nhập-"; } if (String.IsNullOrEmpty(Position)) { InvalidFields += "Chức vụ-"; } if (Password != RepeatPassword) { InvalidFields += "<br />Mật khảu không khớp !!!"; } if (InvalidFields != "") { return(Json(new Result() { CodeError = 1, Data = InvalidFields }, JsonRequestBehavior.AllowGet)); } else { var user = db.Accounts.SingleOrDefault(x => x.ID == ID); try { var rightsSplit = rights.Split(','); var rightRemove = db.Account_Right_Detail.Where(x => x.AccountID == ID).ToList(); foreach (var r in rightRemove) { db.Account_Right_Detail.Remove(r); } db.SaveChanges(); foreach (var r in rightsSplit) { if (!String.IsNullOrEmpty(r)) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = int.Parse(r) }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); if (Convert.ToBoolean(module1).Equals(user.CDVT)) { } else { var listRight = db.Account_Right.Where(x => x.ModuleID == 1 + "").ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='1'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module1 == 0) { } else { db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } } if (Convert.ToBoolean(module2).Equals(user.TCLD)) { } else { var listRight = db.Account_Right.Where(x => x.ModuleID == 2 + "").ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='2'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module2 == 0) { } else { db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } } if (Convert.ToBoolean(module3).Equals(user.KCS)) { } else { var listRight = db.Account_Right.Where(x => x.ModuleID == 3 + "").ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='3'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module3 == 0) { } else { db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } } if (Convert.ToBoolean(module4).Equals(user.DK)) { } else { var listRight = db.Account_Right.Where(x => x.ModuleID == 4 + "").ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='4'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module4 == 0) { } else { db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } } if (Convert.ToBoolean(module5).Equals(user.BGD)) { } else { var listRight = db.Account_Right.Where(x => x.ModuleID == 5 + "").ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='5'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module5 == 0) { } else { db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } } if (Convert.ToBoolean(module6).Equals(user.PXKT)) { } else { var listRight = db.Account_Right.Where(x => x.ModuleID == 6 + "").ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "' and a.ModuleID='6'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module6 == 0) { } else { db.SaveChanges(); foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); } } if (Convert.ToBoolean(module7).Equals(user.ADMIN)) { } else { var listRight = db.Account_Right.ToList(); var rightRemoveup = db.Database.SqlQuery <Account_Right_Detail>("select ar.* from Account_Right a , Account_Right_Detail ar where a.ID = ar.RightID and ar.AccountID='" + ID + "'").ToList <Account_Right_Detail>(); foreach (var r in rightRemoveup) { var del = db.Account_Right_Detail.Where(a => a.ID == r.ID).SingleOrDefault(); db.Account_Right_Detail.Remove(del); } if (module7 == 0) { module1 = 0; module2 = 0; module3 = 0; module4 = 0; module5 = 0; module6 = 0; module7 = 0; } else { foreach (var r in listRight) { if (!String.IsNullOrEmpty(r.ID + "")) { Account_Right_Detail rd = new Account_Right_Detail() { AccountID = ID, RightID = r.ID }; db.Account_Right_Detail.Add(rd); } } db.SaveChanges(); module1 = 1; module2 = 1; module3 = 1; module4 = 1; module5 = 1; module6 = 1; module7 = 1; db.SaveChanges(); } } user.Name = Name; user.Username = Username; if (String.IsNullOrEmpty(Password)) { } else { string passXc = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl"); user.Password = passXc; } user.Position = Position; user.CDVT = Convert.ToBoolean(module1); user.TCLD = Convert.ToBoolean(module2); user.KCS = Convert.ToBoolean(module3); user.DK = Convert.ToBoolean(module4); user.BGD = Convert.ToBoolean(module5); user.PXKT = Convert.ToBoolean(module6); user.ADMIN = Convert.ToBoolean(module7); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } catch (Exception) { return(Json(new Result() { CodeError = 2, Data = "Có lỗi vui lòng kiểm tra lại!" }, JsonRequestBehavior.AllowGet)); } return(Json(new Result() { CodeError = 0, Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được cập nhật thành công cho <strong style='color:black;'>" + Name + "</strong>" }, JsonRequestBehavior.AllowGet)); } }
public JsonResult UpdateUser(int ID, string Name, string Username, string Email, string Password, string RepeatPassword, int module1, int module2, int module3, int module4, int module5, int module8, int module9, int module6, string rights, string locRights1, string locRights2, string locRights3) { if (db.SYS_USER.Where(a => a.ID != ID).Where(a => a.Username == Username).Count() > 0) { return(Json(new Result() { CodeError = 2, Data = "Người dùng với tên đăng nhập <strong style='color:black; '>" + Username + "</strong> đã tồn tại!" }, JsonRequestBehavior.AllowGet)); } if (!String.IsNullOrEmpty(Email) && db.SYS_USER_GOOGLE_ACCOUNT.Where(a => a.ID != ID).Where(a => a.Email == Email).Count() > 0) { return(Json(new Result() { CodeError = 2, Data = "Người dùng với email <strong style='color:black; '>" + Email + "</strong> đã tồn tại!" }, JsonRequestBehavior.AllowGet)); } string InvalidFields = ""; if (String.IsNullOrEmpty(Name)) { InvalidFields += "txtName-"; } //if (String.IsNullOrEmpty(Email)) //{ // InvalidFields += "txtEmail-"; //} if (String.IsNullOrEmpty(Username)) { InvalidFields += "txtUsername-"; } if (!String.IsNullOrEmpty(Password) && Password != RepeatPassword) { InvalidFields += "txtRepeatPassword-"; } if (InvalidFields != "") { return(Json(new Result() { CodeError = 1, Data = InvalidFields }, JsonRequestBehavior.AllowGet)); } else { string passEncrypt = ""; if (!String.IsNullOrEmpty(Password)) { passEncrypt = new XCryptEngine(XCryptEngine.AlgorithmType.MD5).Encrypt(Password, "pl"); } var intOutVal = new ObjectParameter("intOutVal", typeof(int)); db.FPT_SP_ADMIN_UPDATE_USER(ID, 0, Name, Username, passEncrypt, module1, module2, module3, module4, module5, module8, module9, module6, Int32.Parse(Session["UserID"].ToString()), intOutVal); var rightsSplit = rights.Split(','); foreach (var r in rightsSplit) { if (!String.IsNullOrEmpty(r)) { db.FPT_SP_ADMIN_GRANT_RIGHTS(ID, Int32.Parse(r)); } } var locRights1Split = locRights1.Split(','); foreach (var r in locRights1Split) { if (!String.IsNullOrEmpty(r)) { db.SP_ADMIN_GRANT_LOCATIONS(ID, Int32.Parse(r), 1); } } var locRights2Split = locRights2.Split(','); foreach (var r in locRights2Split) { if (!String.IsNullOrEmpty(r)) { db.SP_ADMIN_GRANT_LOCATIONS(ID, Int32.Parse(r), 2); } } var locRights3Split = locRights3.Split(','); foreach (var r in locRights3Split) { if (!String.IsNullOrEmpty(r)) { db.SP_ADMIN_GRANT_LOCATIONS(ID, Int32.Parse(r), 3); } } if (!String.IsNullOrEmpty(Email)) { if (db.SYS_USER_GOOGLE_ACCOUNT.Where(a => a.ID == ID).Count() > 0) { var userGoogleAccountDel = db.SYS_USER_GOOGLE_ACCOUNT.Where(a => a.ID == ID).First(); db.Entry(userGoogleAccountDel).State = EntityState.Deleted; var userGoogleAccount = db.SYS_USER_GOOGLE_ACCOUNT.Create(); userGoogleAccount.ID = ID; userGoogleAccount.Email = Email; db.SYS_USER_GOOGLE_ACCOUNT.Add(userGoogleAccount); } else { var userGoogleAccount = db.SYS_USER_GOOGLE_ACCOUNT.Create(); userGoogleAccount.ID = ID; userGoogleAccount.Email = Email; db.SYS_USER_GOOGLE_ACCOUNT.Add(userGoogleAccount); } } db.SaveChanges(); return(Json(new Result() { CodeError = 0, Data = "Tài khoản <strong style='color:black;'>" + Username + " </strong> đã được cập nhật thành công cho <strong style='color:black;'>" + Name + "</strong>" }, JsonRequestBehavior.AllowGet)); } }