public async Task <ActionResult> SignIn(UserLoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var pwd = UtilEncrypt.GetMd5Hash(model.Password); var acc = _userService.GetAll().FirstOrDefault(a => a.Username.Equals(model.UserName) && a.Password.Equals(pwd)); if (acc != null) { StoreCookie(acc); if (string.IsNullOrWhiteSpace(returnUrl)) { return(RedirectToAction("Index", "Home")); } return(Redirect(returnUrl)); } else { TempData[Constants.NotifyMessage] = new NotifyModel() { Result = false, Message = string.Format(Resource.DataIsNotFound, model.UserName) }; return(RedirectToAction("Login", "Account")); } } TempData[Constants.NotifyMessage] = new NotifyModel() { Result = false, Message = string.Format(Resource.InvalidData, Resource.User) }; return(RedirectToAction("Login", "Account")); }
public ActionResult ChangePassword(ChangePasswordModel model) { if (ModelState.IsValid) { MembershipCreateStatus tt = new MembershipCreateStatus(); // 在某些出错情况下,ChangePassword 将引发异常, // 而不是返回 false。 bool changePasswordSucceeded = false; try { User currentUser = ManageService.UserService().GetUserByUsername(User.Identity.Name); if (model.OldPassword.Equals(model.NewPassword)) { ModelState.AddModelError("", "新密码不能设置和旧密码一样。"); } else if (!currentUser.Password.Equals(UtilEncrypt.MD5Encoding(model.OldPassword))) { ModelState.AddModelError("", "您输入的旧密码不正确!"); } else { changePasswordSucceeded = ManageService.UserService().ChangePassword(currentUser, model.OldPassword, model.NewPassword); changePasswordSucceeded = true; } } catch (Exception ex) { Console.WriteLine(ex.Message);; } if (changePasswordSucceeded) { return(RedirectToAction("ChangePasswordSuccess")); } else { ModelState.AddModelError("", "当前密码不正确或新密码无效。"); } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 return(View(model)); }
public async Task <ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { try { if (!model.Password.Equals(model.ConfirmPassword)) { ModelState["Password"].Errors.Add("Password is not match"); return(RedirectToAction("Login", "Account")); } _userService.Insert(model); var pwd = UtilEncrypt.GetMd5Hash(model.Password); var acc = _userService.GetAll().FirstOrDefault(a => a.Username.Equals(model.UserName) && a.Password.Equals(pwd)); if (acc != null) { StoreCookie(acc); } return(RedirectToAction("Index", "Home")); } catch (Exception ex) { var notify = new NotifyModel() { Result = false, Message = Resource.InternalException }; if (ex.InnerException == null) { notify.Message = ex.Message; } TempData[Constants.NotifyMessage] = notify; return(RedirectToAction("Login", "Account")); } } TempData[Constants.NotifyMessage] = new NotifyModel() { Result = true, Message = string.Format(Resource.InvalidData, Resource.User) }; return(RedirectToAction("Login", "Account")); // If we got this far, something failed, redisplay form }
public void TestMD5() { // 测试1:MD5加密-测试值:admin string md5Str = UtilEncrypt.MD5Encoding("admin"); Assert.AreEqual("21232f297a57a5a743894a0e4a801fc3", md5Str); // 测试2:MD5加密-测试值:iloveu md5Str = UtilEncrypt.MD5Encoding("iloveu"); Assert.AreEqual("edbd0effac3fcc98e725920a512881e0", md5Str); // 测试3:MD5加密加盐 md5Str = UtilEncrypt.MD5Encoding("admin", "admin"); Assert.AreEqual("ceb4f32325eda6142bd65215f4c0f371", md5Str); // 测试4:MD5加密16位 md5Str = UtilEncrypt.MD5Encoding16Bit("iloveu"); Assert.AreEqual("ac3fcc98e725920a", md5Str); }
/// <summary> /// 添加用户 /// </summary> /// <param name="username">用户名称</param> /// <param name="password">密码</param> /// <param name="Email">邮箱地址</param> /// <param name="Cellphone">联系电话</param> /// <returns></returns> public int CreatUser(string UserName, string Password, string Email, string Cellphone) { //1.确认用户名和密码是否为空,如果为空,返回 -1 if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password)) { return(0); } //UNDONE:2.用户名是否无效【如含有特殊符号】 //if (!IsNormalCharacter(UserName)) //{ // return 0; //} //3.电子邮件地址是否已存在,如果存在,返回 7 if (IsEmailExist(Email, null)) { return(7); } //4.确认用户名称是否已经使用过,如果已经使用过,返回 6 if (IsUsernameExist(UserName, null)) { return(6); } else { User user = new User(); user.Username = UserName; //5.密码需要进行加密,采用md5不可逆编码 Password = UtilEncrypt.MD5Encoding(Password); user.Password = Password; user.Email = Email; user.Cellphone = Cellphone; user.LoginTimes = 1; user.CommitTime = DateTime.Now; user.UpdateTime = DateTime.Now; db.User.Add(user); db.SaveChanges(); } return(0); }
/// <summary> /// 校验是否合法用户 /// </summary> /// <param name="UserName">用户名</param> /// <param name="Password">密码</param> /// <returns></returns> public bool IsValidateUser(string UserName, string Password) { Init_Db(); bool result; Password = UtilEncrypt.MD5Encoding(Password); int count = db.User.Where(e => e.Username.Equals(UserName) && e.Password.Equals(Password) ).Count(); if (count == 1) { result = true; } else { result = false; } return(result); }
public async Task <ActionResult> Login(UserLoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var pwd = UtilEncrypt.GetMd5Hash(model.Password); var acc = UnitOfWork.Repository <Account>().GetAll(a => a.Username.Equals(model.UserName) && a.Password.Equals(pwd)).FirstOrDefault(); if (acc != null) { var ma = new MyAccount(); ma.Username = acc.Username; ma.Fullname = acc.FullName; ma.Roles = acc.Role.Name; var fat = new FormsAuthenticationTicket(1, "octopusAuth", DateTime.Now, DateTime.Now.AddMinutes(15), false, JsonConvert.SerializeObject(ma)); var ck = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(fat)) { Expires = DateTime.Now.AddMinutes(15) }; Response.Cookies.Add(ck); if (string.IsNullOrWhiteSpace(returnUrl)) { return(RedirectToAction("Index", "Home", new RouteValueDictionary(Constants.AreaAdmin))); } return(Redirect(returnUrl)); } else { TempData[Constants.NotifyMessage] = new NotifyModel() { Result = false, Message = string.Format(Resource.DataIsNotFound, model.UserName) }; return(RedirectToAction("Login", "Home", new RouteValueDictionary(Constants.AreaAdmin))); } } TempData[Constants.NotifyMessage] = new NotifyModel() { Result = false, Message = string.Format(Resource.InvalidData, Resource.User) }; return(RedirectToAction("Login", "Home", new RouteValueDictionary(Constants.AreaAdmin))); }
/// <summary> /// 修改用户密码 /// </summary> /// <param name="OldPassword">旧密码</param> /// <param name="NewPassword">新密码</param> /// <returns></returns> public bool ChangePassword(User user, string OldPassword, string NewPassword) { try { NewPassword = UtilEncrypt.MD5Encoding(NewPassword); if (user.Password.Equals(NewPassword)) { return(false); } else { user.Password = NewPassword; db.SaveChanges(); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } return(true); }
public Account Login(string username, string password) { password = UtilEncrypt.GetMd5Hash(password); return(Repository.GetAll(a => a.Username.Equals(username) && a.Password.Equals(password)).FirstOrDefault()); }