Пример #1
0
        public virtual async Task <IActionResult> ConfirmEmailChange(string userId, string newEmail, string code)
        {
            ModelState.Clear();

            if (userId == null || code == null)
            {
                return(this.RedirectToSiteRoot(CurrentSite));
            }

            var user = await UserManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{UserManager.GetUserId(User)}'."));
            }

            var model = new ChangeUserEmailViewModel
            {
                HasPassword            = await UserManager.HasPasswordAsync(user),
                AccountApproved        = user.AccountApproved,
                CurrentEmail           = user.Email,
                NewEmail               = newEmail,
                AllowUserToChangeEmail = CurrentSite.AllowUserToChangeEmail,
                EmailIsConfigured      = await SiteCapabilities.SupportsEmailNotification(CurrentSite),
                RequireConfirmedEmail  = CurrentSite.RequireConfirmedEmail
            };

            try
            {
                var siteUrl = Url.Action(new UrlActionContext
                {
                    Action     = "Index",
                    Controller = "Home",
                    Protocol   = HttpContext.Request.Scheme
                });

                var success = await EmailChangeHandler.HandleEmailChangeConfirmation(model, user, newEmail, code, siteUrl);

                if (success)
                {
                    this.AlertSuccess(model.SuccessNotification, true);
                }
                else
                {
                    this.AlertDanger(model.SuccessNotification, true);
                }
            }
            catch (Exception ex)
            {
                this.AlertDanger(StringLocalizer["Error - email could not be changed. Contact the site administrator for support."], true);
                Log.LogError(ex, $"Unexpected error occurred changing email address for user ID '{user.Id}'.");
            }

            return(await Index()); // Route back to Index Page, taking toast alerts along
        }