コード例 #1
0
        public ActionResult Authenticate(UserAccountView account, string returnUrl)
        {
            var user = _userService.AuthenticateUser(account.Email, account.Password);

            if (user.IsAuthenticated)
            {
                SecurityContextManager.Current.CurrentUser        = user.SelectedUser;
                SecurityContextManager.Current.IsAuthenticated    = true;
                SecurityContextManager.Current.CurrentAccessLevel = user.SelectedUser.AccessLevel;

                _formsAuthentications.SetAuthenticationToken(user.SelectedUser.ID.ToString());
                if (!string.IsNullOrEmpty(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
                else
                {
                    return(RedirectToAction("Index", "Home"));
                }
            }
            else
            {
                UserAccountView accountView = InitializeAccountViewWithIssue(true, "Invalid credentials. Please try again.");
                accountView.CallBackSettings.ReturnUrl = "";
                return(View("Index", accountView));
            }
        }
コード例 #2
0
        public ActionResult Login(LoginModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }
            //验证验证码是否正确
            var pwdErrorCount = Session["PwdErrorCount"];

            if (pwdErrorCount != null && Convert.ToInt32(pwdErrorCount) > 5)
            {
                var sessionVerifyCode = Session["VerifyCode"];
                if (sessionVerifyCode == null || model.VerifyCode != sessionVerifyCode.ToString())
                {
                    return(Json(GetResult(false, "验证码输入错误,请刷新重试。", new { errorCount = pwdErrorCount })));
                }
            }
            var entity = _accountRepository.Table.SingleOrDefault(p => p.UserName == model.UserName);

            if (entity == null)
            {
                return(Json(GetResult(false, "用户不存在。")));
            }
            if (entity.Password != model.Password.ToMd5())
            {
                int count      = 1;
                var errorCount = Session["PwdErrorCount"];
                if (errorCount != null)
                {
                    count = Convert.ToInt32(errorCount) + 1;
                }
                Session["PwdErrorCount"] = count;

                return(Json(GetResult(false, "用户名或密码输错了呢。", new { errorCount = count })));
            }
            //记录登录日志
            _loginLogRepository.Insert(new LoginLog
            {
                Ip         = WebHelper.GetIp(),
                CreateTime = DateTime.Now,
                UserAgent  = Request.UserAgent
            });

            //重置错误次数
            Session["PwdErrorCount"] = null;
            //保存身份票据
            _authentication.SetAuthenticationToken(entity.UserName);
            //保存登录名
            if (model.RememberMe)
            {
                HttpCookie cookie = new HttpCookie("UserName");
                cookie.Value   = entity.UserName;
                cookie.Expires = DateTime.Now.AddDays(5);

                Response.Cookies.Set(cookie);
            }
            return(Json(GetResult(true, "登录成功。")));
        }
コード例 #3
0
 public void SetCookie(string username, string userData, bool isCookiePersistent)
 {
     _formsAuthentication.SetAuthenticationToken(username, userData, isCookiePersistent);
 }