Exemple #1
0
        static int RunChangeUserEmail(ChangeUserEmail opts)
        {
            var sp = GetServiceProvider();

            ConsoleTasks.ChangeUserEmail(sp, opts.OldEmail, opts.NewEmail).GetAwaiter().GetResult();
            return(0);
        }
Exemple #2
0
        public async Task given_current_and_new_email_should_be_changed()
        {
            var command = new ChangeUserEmail()
            {
                CurrentEmail = "*****@*****.**",
                NewEmail     = "*****@*****.**"
            };
            var payload  = GetPayload(command);
            var response = await Client.PutAsync("account/email", payload);

            response.StatusCode.Should().BeEquivalentTo(HttpStatusCode.NoContent);
        }
        public async Task <IActionResult> Put(ChangeUserEmail command)
        {
            await DispatchAsync(command);

            return(NoContent());
        }
Exemple #4
0
        public async Task <IActionResult> EditUserEmail(ChangeUserEmail userEmail)
        {
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError(string.Empty, $"Unexpected error: Not all fields fulfilled the required format of {ModelState}.");

                return(View());
            }

            if (await _userManager.FindByEmailAsync(userEmail.OldEmail) == null)
            {
                ModelState.AddModelError(string.Empty, "Unexpected Error Occurred: No user exists with this email.");

                ViewBag.error = $"Unexpected error occurred: Could not find user with the given email: {userEmail.OldEmail}.";

                return(View());
            }

            if (userEmail.OldEmail != User.Identity.Name)
            {
                ModelState.AddModelError(string.Empty, "Unexpected error occurred: Something went wrong.");

                return(View());
            }

            if (userEmail.OldEmail == userEmail.NewEmail)
            {
                ModelState.AddModelError(string.Empty, "Unexpected error occurred: The new email can not be the same as the old one.");

                ViewBag.error = "Unexpected error occurred: The new email cannot be the same as the old one.";

                return(View());
            }

            if (await _userManager.FindByEmailAsync(userEmail.NewEmail) != null)
            {
                ModelState.AddModelError(string.Empty, new IdentityErrorDescriber().DuplicateEmail(userEmail.NewEmail).Description);

                ViewBag.error = "Unexpected error occurred: Invalid email.";

                return(View());
            }

            var token = await _userManager.GenerateChangeEmailTokenAsync(await _userManager.FindByEmailAsync(userEmail.OldEmail), userEmail.NewEmail);

            if (string.IsNullOrWhiteSpace(token))
            {
                ModelState.AddModelError(string.Empty, new IdentityErrorDescriber().InvalidToken().Description);

                ViewBag.error = "Unexpected error occurred: Could not token. Did you submit a valid email?";

                return(View());
            }

            var callbackUrl = Url.Action(new UrlActionContext
            {
                Action     = nameof(VerifyEmailChangeTokenAsync),
                Controller = "Account",
                Values     = new
                {
                    userId = await _userManager.GetUserIdAsync(await _userManager.FindByEmailAsync(userEmail.NewEmail)),
                    email  = userEmail.NewEmail,
                    token
                },
                Protocol = Request.Scheme = "https",
                Host     = "localhost:44351"
            });

            var htmlMessage = await _emailSender.EmailVerificationMessageAsync(callbackUrl, Request, Url);

            await _emailSender.SendEmailAsync(userEmail.NewEmail, "Confirm your email", htmlMessage);

            return(View());
        }