예제 #1
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
                return RedirectToAction("index");

            UserModel accountModel = new UserModel();
            if (ModelState.IsValid)
            {
                var userInfo = accountModel.ValidateUser(model.UserName, model.Password);
                if (userInfo != null)
                {
                    //设置验证用户的cookie信息
                    accountModel.SignIn(userInfo, model.RememberMe);

                    if (Url.IsLocalUrl(returnUrl))
                        return Json(new { key = 1, value = returnUrl }, JsonRequestBehavior.AllowGet);
                    else
                        return Json(new { key = 1, value = "/" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { key = 0, value = "账号或密码错误" }, JsonRequestBehavior.AllowGet);
                }
            }

            return View(model);
        }