예제 #1
0
        public virtual async Task <IActionResult> Register(string returnUrl = null)
        {
            if (AccountService.IsSignedIn(User))
            {
                return(this.RedirectToSiteRoot(CurrentSite));
            }
            if (!CurrentSite.AllowNewRegistration)
            {
                return(new StatusCodeResult(404));
            }
            // login is equivalent to register for new social auth users
            // if db auth is disabled just redirect
            if (CurrentSite.DisableDbAuth && CurrentSite.HasAnySocialAuthEnabled())
            {
                return(RedirectToAction("Login"));
            }

            ViewData["Title"] = StringLocalizer["Register"];

            returnUrl = IdentityServerIntegration.EnsureFolderSegmentIfNeeded(CurrentSite, returnUrl);
            //identityserver integration point
            var idProvider = await IdentityServerIntegration.GetAuthorizationContextAsync(returnUrl);

            if (!string.IsNullOrEmpty(idProvider))
            {
                // if IdP is passed, then bypass showing the login screen
                return(ExternalLogin(idProvider, returnUrl));
            }
            ViewData["ReturnUrl"] = returnUrl;

            var model = new RegisterViewModel
            {
                SiteId = CurrentSite.Id
            };

            if ((CurrentSite.CaptchaOnRegistration) && (!string.IsNullOrWhiteSpace(CurrentSite.RecaptchaPublicKey)))
            {
                model.RecaptchaSiteKey    = CurrentSite.RecaptchaPublicKey;
                model.UseInvisibleCaptcha = CurrentSite.UseInvisibleRecaptcha;
            }
            model.UseEmailForLogin           = CurrentSite.UseEmailForLogin;
            model.RegistrationPreamble       = CurrentSite.RegistrationPreamble;
            model.RegistrationAgreement      = CurrentSite.RegistrationAgreement;
            model.AgreementRequired          = !string.IsNullOrWhiteSpace(CurrentSite.RegistrationAgreement);
            model.ExternalAuthenticationList = await AccountService.GetExternalAuthenticationSchemes();

            var viewName = await CustomRegistration.GetRegisterViewName(CurrentSite, HttpContext);

            await CustomRegistration.HandleRegisterGet(
                CurrentSite,
                model,
                HttpContext,
                ViewData);

            return(View(viewName, model));
        }
예제 #2
0
        public virtual async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["Title"] = StringLocalizer["Register"];

            ViewData["ReturnUrl"] = returnUrl;

            Analytics.HandleRegisterSubmit("Onsite").Forget();

            if ((CurrentSite.CaptchaOnRegistration) && (!string.IsNullOrWhiteSpace(CurrentSite.RecaptchaPublicKey)))
            {
                model.RecaptchaSiteKey    = CurrentSite.RecaptchaPublicKey;
                model.UseInvisibleCaptcha = CurrentSite.UseInvisibleRecaptcha;
            }
            model.UseEmailForLogin           = CurrentSite.UseEmailForLogin;
            model.RegistrationPreamble       = CurrentSite.RegistrationPreamble;
            model.RegistrationAgreement      = CurrentSite.RegistrationAgreement;
            model.AgreementRequired          = !string.IsNullOrWhiteSpace(CurrentSite.RegistrationAgreement);
            model.ExternalAuthenticationList = await AccountService.GetExternalAuthenticationSchemes();

            bool isValid = ModelState.IsValid;

            bool customDataIsValid = await CustomRegistration.HandleRegisterValidation(
                CurrentSite,
                model,
                HttpContext,
                ViewData,
                ModelState);

            if (isValid && customDataIsValid)
            {
                if ((CurrentSite.CaptchaOnRegistration) && (!string.IsNullOrWhiteSpace(CurrentSite.RecaptchaPublicKey)))
                {
                    var captchaResponse = await RecaptchaServerSideValidator.ValidateRecaptcha(Request, CurrentSite.RecaptchaPrivateKey);

                    if (!captchaResponse.Success)
                    {
                        ModelState.AddModelError("recaptchaerror", StringLocalizer["reCAPTCHA Error occured. Please try again"]);
                        isValid = false;
                    }
                }

                if (!string.IsNullOrWhiteSpace(CurrentSite.RegistrationAgreement))
                {
                    if (!model.AgreeToTerms)
                    {
                        ModelState.AddModelError("agreementerror", StringLocalizer["You must agree to the terms"]);
                        isValid = false;
                    }
                }

                var viewName = await CustomRegistration.GetRegisterViewName(CurrentSite, HttpContext);

                if (!isValid || !customDataIsValid)
                {
                    return(View(viewName, model));
                }

                var result = await AccountService.TryRegister(model, ModelState, HttpContext, CustomRegistration);

                if (result.SignInResult.Succeeded || (result.SignInResult.IsNotAllowed && result.User != null))
                {
                    await CustomRegistration.HandleRegisterPostSuccess(
                        CurrentSite,
                        model,
                        HttpContext,
                        result);

                    if (result.SignInResult.Succeeded)
                    {
                        return(await HandleLoginSuccess(result, returnUrl));
                    }
                }

                foreach (var reason in result.RejectReasons)
                {
                    //these reasons are not meant to be shown in the ui
                    // but we can log them so admin will see failed attempts in the log along with reasons
                    Log.LogWarning(reason);
                }

                if (result.SignInResult.IsNotAllowed)
                {
                    return(await HandleLoginNotAllowed(result, returnUrl));
                }

                var te = result.RejectReasons.FirstOrDefault();
                if (string.IsNullOrEmpty(te))
                {
                    te = "unknown";
                }

                Analytics.HandleRegisterFail("Onsite", te).Forget();

                Log.LogInformation($"registration did not succeed for {model.Email}");
                // ModelState.AddModelError(string.Empty, sr["Invalid registration attempt."]);
                return(View(viewName, model));
            }

            var errors       = ModelState.Keys.Where(k => ModelState[k].Errors.Count > 0).Select(k => new { propertyName = k, errorMessage = ModelState[k].Errors[0].ErrorMessage });
            var trackedError = errors.FirstOrDefault().errorMessage;

            Analytics.HandleRegisterFail("Onsite", trackedError).Forget();

            // If we got this far, something failed, redisplay form
            return(View(model));
        }