예제 #1
0
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                int userId = AccountBLL.ValidateAccount(model.Email, model.Password);

                if (userId > 0)
                {
                    //验证成功,用户名密码正确,构造用户数据
                    var userData = new HttpUserDataPrincipal {
                        UserId = userId, UserName = model.Email
                    };

                    //保存Cookie
                    HttpFormsAuthentication <HttpUserDataPrincipal> .SetAuthCookie(model.Email, userData, model.RememberMe);

                    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(model));
        }
예제 #2
0
        public ActionResult Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                int userId = AccountBLL.Add(model.Email, model.Password);

                if (userId > 0)
                {
                    //注册成功,用户名密码正确,构造用户数据
                    var userData = new HttpUserDataPrincipal {
                        UserId = userId, UserName = model.Email
                    };

                    //保存Cookie
                    HttpFormsAuthentication <HttpUserDataPrincipal> .SetAuthCookie(model.Email, userData, false);

                    return(RedirectToAction("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "用户名已存在。");
                }
            }

            return(View(model));
        }