示例#1
0
        public ActionResult Register(Models.User user)
        {
            if (ModelState.IsValid)
            {
                //Is the email address valid?
                if (ValidEmail(user.email))
                {
                    //If so, does the email exist already?
                    if (user.EmailExists(user.email))
                    {
                        ModelState.AddModelError("Email", "Email address already in use");
                    }
                }

                else
                {
                    ModelState.AddModelError("Email", "Email address is invalid");
                }

                if (user.password.Length < 5 || user.password.Length > 30)
                {
                    ModelState.AddModelError("Password", "Password must be between 5 and 30 characters");
                }
            }

            if (ModelState.IsValid)
            {
                if (user.Register(user.email, user.password))
                {
                    //Get the ID of the user
                    int userId = user.GetUserId(user.email);
                    int admin  = user.GetUserRole(user.email);
                    FormsAuthentication.SetAuthCookie(userId.ToString(), true);
                    Session["userID"] = userId;
                    Session["email"]  = user.email;
                    Session["admin"]  = admin;
                    return(RedirectToAction("Index", "Home"));
                }

                else
                {
                    ModelState.AddModelError("", "Registration failed");
                }
            }
            return(View(user));
        }
示例#2
0
 public ActionResult Login(Models.User user)
 {
     if (ModelState.IsValid)
     {
         if (user.CheckLogon(user.email, user.password))
         {
             FormsAuthentication.SetAuthCookie(user.email, true);
             //Get the ID of the user
             int userId = user.GetUserId(user.email);
             int admin  = user.GetUserRole(user.email);
             FormsAuthentication.SetAuthCookie(userId.ToString(), true);
             Session["userID"] = userId;
             Session["email"]  = user.email;
             Session["admin"]  = admin;
             return(RedirectToAction("Index", "Home"));
         }
         else
         {
             ModelState.AddModelError("", "Login details were incorrect");
         }
     }
     return(View(user));
 }