コード例 #1
0
        public ActionResult Register(User u)
        {
            try
            {
                if (u.Username == null || u.Password == null || u.Email == null || u.Country == null)
                {
                    throw new CustomException("Please fill all fields");
                }
                else
                {
                    UsersBL uBl = new UsersBL();
                    if (uBl.UserAlreadyExists(u.Username))
                    {
                        throw new CustomException("Username Already Exists");
                    }
                    else if (uBl.EmailAlreadyExists(u.Email))
                    {
                        throw new CustomException("Email Already Exists");
                    }
                    else
                    {
                        CaptchaResponse response = ValidateCaptcha(Request.Form["g-recaptcha-response"]);


                        if (response.Success && ModelState.IsValid) //ModelState.isValid: if an input passed is not like the model it will return false //response.Success (captcha resposne)
                        {
                            uBl.Register(u);
                            TempData["message"] = "Account Successfully Registered.";
                            Logger.Log("Guest", Request.Path, "Registered as " + u.Username);
                            return(RedirectToAction("Login", "Accounts"));
                        }
                        else
                        {
                            throw new CustomException("CAPTCHA Error: " + response.ErrorMessage[0].ToString());
                        }
                    }
                }
            }

            catch (CustomException ex) {
                TempData["errormessage"] = ex.Message;
                Logger.Log(User.Identity.Name, Request.Path, ex.Message);
                return(View());
            }

            catch (Exception ex)
            {
                TempData["errormessage"] = ex.Message;
                Logger.Log(User.Identity.Name, Request.Path, ex.Message);
                return(View());
            }
        }