예제 #1
0
        private async Task <IActionResult> HandleLoginNotAllowed(UserLoginResult result, string returnUrl)
        {
            _analytics.HandleLoginNotAllowed(result).Forget();

            if (result.User != null)
            {
                await _ipAddressTracker.TackUserIpAddress(_currentSite.Id, result.User.Id);

                if (result.NeedsEmailConfirmation)
                {
                    if (ShouldSendConfirmation(result.User))
                    {
                        var callbackUrl = Url.Action(new UrlActionContext
                        {
                            Action     = "ConfirmEmail",
                            Controller = "Account",
                            Values     = new { userId = result.User.Id.ToString(), code = result.EmailConfirmationToken, returnUrl },
                            Protocol   = HttpContext.Request.Scheme
                        });

                        _emailSender.SendAccountConfirmationEmailAsync(
                            _currentSite,
                            result.User.Email,
                            _sr["Confirm your account"],
                            callbackUrl,
                            result.EmailConfirmationToken
                            ).Forget();


                        this.AlertSuccess(_sr["Please check your email inbox, we just sent you a link that you need to click to confirm your account"], true);
                    }

                    return(RedirectToAction("EmailConfirmationRequired", new { userId = result.User.Id, didSend = true, returnUrl }));
                }

                if (result.NeedsAccountApproval)
                {
                    var timeSpan = DateTime.UtcNow - result.User.CreatedUtc;
                    if (timeSpan.TotalDays < 1)
                    {
                        // account was just created so send notification to approver
                        _emailSender.AccountPendingApprovalAdminNotification(_currentSite, result.User).Forget();
                    }

                    return(RedirectToAction("PendingApproval", new { userId = result.User.Id, didSend = true }));
                }
            }

            return(this.RedirectToSiteRoot(_currentSite));
        }
예제 #2
0
        public async Task <IActionResult> Register(RegisterViewModel model)
        {
            ViewData["Title"] = sr["Register"];
            if ((Site.CaptchaOnRegistration) && (Site.RecaptchaPublicKey.Length > 0))
            {
                model.RecaptchaSiteKey = Site.RecaptchaPublicKey;
            }
            model.UseEmailForLogin           = Site.UseEmailForLogin;
            model.RegistrationPreamble       = Site.RegistrationPreamble;
            model.RegistrationAgreement      = Site.RegistrationAgreement;
            model.AgreementRequired          = Site.RegistrationAgreement.Length > 0;
            model.ExternalAuthenticationList = signInManager.GetExternalAuthenticationSchemes();

            bool isValid = ModelState.IsValid;

            if (isValid)
            {
                if ((Site.CaptchaOnRegistration) && (Site.RecaptchaPublicKey.Length > 0))
                {
                    string recpatchaSecretKey = Site.RecaptchaPrivateKey;

                    var captchaResponse = await this.ValidateRecaptcha(Request, recpatchaSecretKey);

                    if (!captchaResponse.Success)
                    {
                        //if (captchaResponse.ErrorCodes.Count <= 0)
                        //{
                        //    return View(model);
                        //}

                        ////TODO: log these errors rather than show them in the ui
                        //var error = captchaResponse.ErrorCodes[0].ToLower();
                        //switch (error)
                        //{
                        //    case ("missing-input-secret"):
                        //        ModelState.AddModelError("recaptchaerror", "The secret parameter is missing.");
                        //        break;
                        //    case ("invalid-input-secret"):
                        //        ModelState.AddModelError("recaptchaerror", "The secret parameter is invalid or malformed.");
                        //        break;
                        //    case ("missing-input-response"):
                        //        ModelState.AddModelError("recaptchaerror", "The response parameter is missing.");
                        //        break;
                        //    case ("invalid-input-response"):
                        //        ModelState.AddModelError("recaptchaerror", "The response parameter is invalid or malformed.");
                        //        break;
                        //    default:
                        //        ModelState.AddModelError("recaptchaerror", "Error occured. Please try again");
                        //        break;
                        //}

                        ModelState.AddModelError("recaptchaerror", "reCAPTCHA Error occured. Please try again");
                        isValid = false;
                    }
                }

                if (Site.RegistrationAgreement.Length > 0)
                {
                    if (!model.AgreeToTerms)
                    {
                        ModelState.AddModelError("agreementerror", sr["You must agree to the terms"]);
                        isValid = false;
                    }
                }

                var userName          = model.Username.Length > 0 ? model.Username : model.Email.Replace("@", string.Empty).Replace(".", string.Empty);
                var userNameAvailable = await userManager.LoginIsAvailable(Guid.Empty, userName);

                if (!userNameAvailable)
                {
                    ModelState.AddModelError("usernameerror", sr["Username not accepted please try a different value"]);
                    isValid = false;
                }

                if (!isValid)
                {
                    return(View(model));
                }

                var user = new SiteUser
                {
                    UserName        = userName,
                    Email           = model.Email,
                    FirstName       = model.FirstName,
                    LastName        = model.LastName,
                    DisplayName     = model.DisplayName,
                    AccountApproved = Site.RequireApprovalBeforeLogin ? false : true
                };

                if (model.DateOfBirth.HasValue)
                {
                    user.DateOfBirth = model.DateOfBirth.Value;
                }

                var result = await userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await ipAddressTracker.TackUserIpAddress(Site.Id, user.Id);

                    if (Site.RequireConfirmedEmail) // require email confirmation
                    {
                        var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                        var callbackUrl = Url.Action(new UrlActionContext {
                            Action     = "ConfirmEmail",
                            Controller = "Account",
                            Values     = new { userId = user.Id.ToString(), code = code },
                            Protocol   = HttpContext.Request.Scheme
                        });

                        emailSender.SendAccountConfirmationEmailAsync(
                            Site,
                            model.Email,
                            sr["Confirm your account"],
                            callbackUrl).Forget();

                        if (this.SessionIsAvailable())
                        {
                            this.AlertSuccess(sr["Please check your email inbox, we just sent you a link that you need to click to confirm your account"], true);

                            return(Redirect("/"));
                        }
                        else
                        {
                            return(RedirectToAction("EmailConfirmationRequired", new { userId = user.Id, didSend = true }));
                        }
                    }
                    else
                    {
                        if (Site.RequireApprovalBeforeLogin)
                        {
                            emailSender.AccountPendingApprovalAdminNotification(Site, user).Forget();
                            return(RedirectToAction("PendingApproval", new { userId = user.Id, didSend = true }));
                        }
                        else
                        {
                            await signInManager.SignInAsync(user, isPersistent : false);

                            return(this.RedirectToSiteRoot(Site));
                        }
                    }
                }
                AddErrors(result);
            }

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