public ActionResult Login(LoginDto dto) { dto.CheckNotNull("dto"); OperationResult result = new OperationResult(OperationResultType.ValidError); if (ModelState.IsValid) { try { if (Session["ValidateCode"] == null|| !dto.CheckCode.ToLower().Equals(Session["ValidateCode"].ToString().ToLower())) { ModelState.AddModelError("CheckCode", "验证码不正确!"); } else { //CommunicationCryptor cryptor = new CommunicationCryptor("", "", "SHA1"); //dto.LoginPwd = cryptor.EncryptData(dto.LoginPwd); result = IdentityContract.CheckLogin(dto); if (result.ResultType == OperationResultType.Success) { User user = result.Data as User; AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie).SetClaimsIdentity(user.Id.ToString(), user.UserName,user.NickName, null )); return RedirectToAction("Index", "Home", new { }); } else { ModelState.AddModelError("LoginName", result.Message); } } } catch (Exception ex) { ModelState.AddModelError("Exception", ex.Message); } } ViewBag.ErrorsMessage = GetModelErrors(ModelState); return View(dto); }
public ActionResult Index(LoginDto dto) { if(dto.LoginName==null) return View(dto); return Login(dto); }
/// <summary> /// 检测用户登录 /// </summary> /// <param name="dto">包含登录的信息Dto</param> /// <returns>业务操作结果</returns> public OperationResult CheckLogin(LoginDto dto) { OperationResult re = new OperationResult(OperationResultType.NoChanged); var user = UserRepository.Entities.FirstOrDefault(c => c.UserName == dto.LoginName); if (user!=null) { if (user.IsLocked == false) { if (UserManager.PasswordHasher.VerifyHashedPassword(user.PasswordHash, dto.LoginPwd) == PasswordVerificationResult.Success) { re.ResultType = OperationResultType.Success; re.Message = "登录成功!"; re.Data = user; } else { re.ResultType = OperationResultType.ValidError; re.Message = "密码错误!"; } } else { re.ResultType = OperationResultType.ValidError; re.Message = "当前用户已经禁用,无法登录,请联系管理员!"; } } else { re.ResultType = OperationResultType.ValidError; re.Message = "系统不存在此用户!"; } return re; }