Ejemplo n.º 1
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                myDatabaseEntities db = new myDatabaseEntities();
                var q = from t in db.user
                        where t.userName == model.UserName
                        where t.userPassword == model.Password
                        select t;

                if (q.Count()==1)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    Session["LoginUser"] = q.Single();
                    if (Url.IsLocalUrl(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "提供的用户名或密码不正确。");
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // 尝试注册用户
                myDatabaseEntities db = new myDatabaseEntities();
                user rgUser = new user();
                var q = from t in db.user
                        where t.userName == model.UserName
                        select t;
                if (q.Count() > 0)
                {
                    ModelState.AddModelError("", "用户名已被注册!");
                }
                else
                {
                    rgUser.userName = model.UserName;
                    rgUser.userNickname = model.NickName;
                    rgUser.userPassword = model.Password;
                    db.user.Add(rgUser);
                    db.SaveChanges();
                    return View("RegisterSuccess");
                }
            }

            // 如果我们进行到这一步时某个地方出错,则重新显示表单
            return View(model);
        }