示例#1
0
        public ActionResult Register(FormCollection values)
        {
            if (CurrentUser != null)
            {
                RedirectToAction("ViewProfile", new { id = CurrentUser.UserID });
            }

            var registration = new RegistrationModel
            {
                Username         = values["Username"],
                Password         = values["Password"],
                PasswordConfirm  = values["PasswordConfirm"],
                Email            = values["Email"],
                SecurityQuestion = values["SecurityQuestion"],
                SecurityAnswer   = values["SecurityAnswer"],
                Gender           = values["Gender"] != null ? values["Gender"][0] : '\0',
                ShowEmail        = values["ShowEmail"].StartsWith("true"),
                ShowGender       = values["ShowGender"].StartsWith("true"),
            };

            if (!registration.IsValid)
            {
                foreach (var violation in registration.GetRuleViolations())
                {
                    this.ModelState.AddModelError(violation.PropertyName, violation.ErrorMessage);
                }

                return(View("Register", registration));
            }

            if (this.Config.UseRecaptcha && !this.IsCaptchaValid(Config.RecaptchaPrivateKey))
            {
                this.ModelState.AddModelError("Captcha", "Captcha validation failed.");

                return(View("Register", registration));
            }

            User newlyRegisteredUser;

            try
            {
                newlyRegisteredUser = this.Register(
                    registration.Username,
                    registration.Password,
                    registration.Email,
                    registration.SecurityQuestion,
                    registration.SecurityAnswer,
                    registration.Gender,
                    registration.ShowEmail,
                    registration.ShowGender,
                    Request,
                    Response);
            }
            catch (Exception ex)
            {
                this.ModelState.AddModelError("Form", ex.Message);
                return(View("Register", registration));
            }

            if (!this.Config.ConfirmEmail && this.Config.LoginAfterRegister)
            {
                this.CurrentUser = newlyRegisteredUser;
            }

            return(View("RegistrationSuccessful"));
        }