コード例 #1
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));
        }
コード例 #2
0
        public virtual async Task <IActionResult> Login(LoginViewModel model, string returnUrl = null)
        {
            ViewData["Title"]     = StringLocalizer["Log In"];
            ViewData["ReturnUrl"] = returnUrl;

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

            var recaptchaKeys = await RecaptchaKeysProvider.GetKeys().ConfigureAwait(false);

            if ((CurrentSite.CaptchaOnLogin) && (!string.IsNullOrEmpty(recaptchaKeys.PublicKey)))
            {
                model.RecaptchaSiteKey    = recaptchaKeys.PublicKey;
                model.UseInvisibleCaptcha = recaptchaKeys.Invisible;
            }
            model.UseEmailForLogin           = CurrentSite.UseEmailForLogin;
            model.LoginInfoTop               = CurrentSite.LoginInfoTop;
            model.LoginInfoBottom            = CurrentSite.LoginInfoBottom;
            model.ExternalAuthenticationList = await AccountService.GetExternalAuthenticationSchemes();

            // don't disable db auth if there are no social auth providers configured
            model.DisableDbAuth = CurrentSite.DisableDbAuth && CurrentSite.HasAnySocialAuthEnabled();

            if (!ModelState.IsValid)
            {
                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.HandleLoginFail("Onsite", trackedError).Forget();

                return(View(model));
            }

            if ((CurrentSite.CaptchaOnLogin) && (!string.IsNullOrEmpty(recaptchaKeys.PrivateKey)))
            {
                var captchaResponse = await RecaptchaServerSideValidator.ValidateRecaptcha(Request, CurrentSite.RecaptchaPrivateKey);

                if (!captchaResponse.Success)
                {
                    Analytics.HandleLoginFail("Onsite", "reCAPTCHA Error").Forget();
                    ModelState.AddModelError("recaptchaerror", StringLocalizer["reCAPTCHA Error occured. Please try again"]);
                    return(View(model));
                }
            }

            var result = await AccountService.TryLogin(model);

            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));
            }

            if (result.SignInResult.RequiresTwoFactor)
            {
                return(await HandleRequiresTwoFactor(result, returnUrl, model.RememberMe));
            }

            if (result.SignInResult.IsLockedOut)
            {
                return(await HandleLockout(result));
            }
            else
            {
                Analytics.HandleLoginFail("Onsite", StringLocalizer["Invalid login attempt."]).Forget();

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