public ActionResult Login(LoginDto model, string returnUrl) { if (ModelState.IsValid) { //string returnUrl = ViewBag.ReturnUrl; OperationResult result=_userService.Login(model); if (result.ResultType == OperationResultType.Success) { if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "用户名或密码不正确!"); } } return View(); }
public OperationResult Login(LoginDto dto) { PublicHelper.CheckArgument(dto, "LoginDto"); //检查参数 ObjectsMapper<LoginDto, User> mapper = ObjectMapperManager.DefaultInstance.GetMapper<LoginDto, User>(); User user = mapper.Map(dto); var account=_userRepository.ReadEntities.SingleOrDefault(a => a.UserName == user.UserName && a.Password == user.Password); if (account == null) { return new OperationResult(OperationResultType.Warning, "登录的用户名或密码错误。"); } var userinfo = new UserInfo() { UserId=account.Id, UserName=account.UserName, NickName=account.NickName, RoleId = string.Join(",", account.Roles), Email=account.Email, Mobile=account.Mobile }; DateTime expiration = dto.RememberMe? DateTime.Now.AddDays(7) : DateTime.Now.Add(FormsAuthentication.Timeout); FormsPrincipal<UserInfo>.SignIn(dto.UserName, userinfo, expiration); // //FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); return new OperationResult(OperationResultType.Success, "登录成功。"); }