public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }
        public ActionResult Index(LoginModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.Username == "ben" && model.Password == "ben")
                {
                    FormsAuthentication.SetAuthCookie(model.Username, true);
                    //if true, user are also in logged in state,although they close their web.
                    return RedirectToAction("Index", "Profile");

                }
                else
                {
                    ModelState.AddModelError("", "Invalid username and password");
                }
            }
            return View();
        }
Exemple #3
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            ModelState.AddModelError("", "提供的用户名或密码不正确。");
            return View(model);
        }
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                // Check if Person exist? Create!
                int id = (int) WebSecurity.GetUserId(model.UserName);
                var person = db.Person.Where(a => a.UserId == id).FirstOrDefault();
                if (person == null)
                {
                    db.Person.Add(new Person() {UserId = WebSecurity.GetUserId(model.UserName)});
                    db.SaveChanges();
                }

                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }