public ActionResult SubmitAdminLogin(Admin user) { AdminsDal dal = new AdminsDal(); List <Admin> users = dal.users.ToList <Admin>(); Encryption enc = new Encryption(); bool exists = false; foreach (Admin u in users) { if (u.username == user.username && enc.ValidatePassword(user.password, u.password)) { exists = true; break; } } if (exists == true) { FormsAuthentication.SetAuthCookie("cookie", true); Session["username"] = user.username; Session["loggedOn"] = "true"; Session["userType"] = "admin"; TempData["LoginStatus"] = null; return(RedirectToAction("Index", "Home")); } else { TempData["LoginStatus"] = "Username or Password are incorrect."; return(View("AdminLogin", user)); } }
/// <summary> /// 添加一条记录 /// </summary> public ResultSet Add(Admins entity) { Func <Admins, ResultStatus> validate = (_entity) => { return(new ResultStatus()); }; Func <Admins, ResultStatus> op = (_entity) => { int ret = new AdminsDal().Add(entity); if (ret > 0) { return(new ResultStatus()); } else { return new ResultStatus() { Success = false, Code = StatusCollection.AddFailed.Code, Description = StatusCollection.AddFailed.Description } }; }; return(HandleBusiness(entity, op, validate)); }
/// <summary> /// 获取所有 /// </summary> public ResultSet <Page <Admins> > GetAll(string fields, int pageIndex, int pageSize, string where, object param, string orderBy) { Func <string, int, int, string, object, string, ResultStatus> validate = (_fields, _pageIndex, _pageSize, _where, _param, _orderBy) => { if (_pageIndex <= 0) { return new ResultStatus() { Code = StatusCollection.ParameterError.Code, Description = "参数 pageIndex 必须大于0", Success = false } } ; if (_pageSize <= 0 || _pageSize > 100) { return new ResultStatus() { Code = StatusCollection.ParameterError.Code, Description = "参数 pageSize 必须大于0,且小于等于100", Success = false } } ; return(new ResultStatus()); }; Func <string, int, int, string, object, string, Page <Admins> > op = (_fields, _pageIndex, _pageSize, _where, _param, _orderBy) => { int recordCount = 0; IList <Admins> list = new AdminsDal().GetAll(_fields, _pageIndex, _pageSize, _where, _param, _orderBy, out recordCount); return(new Page <Admins>(_pageIndex, _pageSize, recordCount, list)); }; return(HandleBusiness(fields, pageIndex, pageSize, where, param, orderBy, op, validate)); }
public ActionResult SubmitAdminRegister(Admin admin) { AdminsDal dal = new AdminsDal(); Encryption enc = new Encryption(); if (ModelState.IsValid) { var isUserAlreadyExists = dal.Users.Any(x => x.UserName == admin.UserName); string hashedPassword = enc.CreateHash(admin.Password); if (isUserAlreadyExists) { TempData["LoginStatus"] = "Username already exists."; return(View("AddAdmin", admin)); } admin.Password = hashedPassword; dal.Users.Add(admin); dal.SaveChanges(); TempData["LoginStatus"] = null; return(RedirectToAction("MyPage", "Home")); } return(View("AddAdmin", admin)); }
public ActionResult SubmitAdminRegister(Admin admin) { AdminsDal dal = new AdminsDal(); Encryption enc = new Encryption(); if (ModelState.IsValid) { try { string hashedPassword = enc.CreateHash(admin.password); admin.password = hashedPassword; dal.users.Add(admin); dal.SaveChanges(); TempData["LoginStatus"] = null; } catch (DbUpdateException e) { TempData["LoginStatus"] = "Username already exists."; return(View("AddAdmin", admin)); } return(RedirectToAction("Index", "Home")); } return(View("AddAdmin", admin)); }
/// <summary> /// 更新 /// </summary> public ResultSet Update(string fields, object param, string where) { Func <string, object, string, ResultStatus> validate = (_fields, _param, _where) => { if (String.IsNullOrEmpty(_fields)) { return new ResultStatus() { Code = StatusCollection.ParameterError.Code, Description = "参数 fields 不能为空", Success = false } } ; if (_param == null) { return new ResultStatus() { Code = StatusCollection.ParameterError.Code, Description = "参数 param 不能为空", Success = false } } ; return(new ResultStatus()); }; Func <string, object, string, ResultStatus> op = (_fields, _param, _where) => { int ret = new AdminsDal().Update(_fields, _param, _where); if (ret > 0) { return(new ResultStatus()); } else { return new ResultStatus() { Success = false, Code = StatusCollection.UpdateFailed.Code, Description = StatusCollection.UpdateFailed.Description } }; }; return(HandleBusiness(fields, param, where, op, validate)); } #endregion #region Extend /// <summary> /// 通过登录名查询 /// </summary> public ResultSet <Admins> GetAdminByLogName(string logName) { Func <string, ResultStatus> validate = (_logName) => { if (string.IsNullOrEmpty(_logName)) { return new ResultStatus() { Success = false, Code = StatusCollection.ParameterError.Code, Description = "参数 logName不能为空" } } ; return(new ResultStatus()); }; Func <string, Admins> op = (_logName) => { return(new AdminsDal().GetAdminByLogName(_logName)); }; return(HandleBusiness <string, Admins>(logName, op, validate)); } #endregion } }