Beispiel #1
0
        public async Task <IActionResult> ResendConfirmationEmail([FromRoute] string userId,
                                                                  [FromQuery] string returnUrl, CancellationToken cancellationToken)
        {
            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(RedirectToRoute("Home"));
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.RouteUrl("ConfirmEmailAddress", new { userId = user.Id, token, returnUrl },
                                           Request.Scheme);
            var email = new ConfirmEmailAddressEmail
                        (
                user.NewEmailAddress ?? user.EmailAddress,
                callbackUrl
                        );

            await _emailSender.SendEmailAsync(email, cancellationToken);

            return(RedirectToRoute("EmailAddressConfirmationSent", new { userId = user.Id, resent = true, returnUrl }));
        }
        public async Task <IActionResult> Register([FromForm] RegisterInputModel inputModel,
                                                   [FromQuery] string returnUrl, CancellationToken cancellationToken)
        {
            RegisterViewModel viewModel;

            if (!ModelState.IsValid)
            {
                viewModel = BuildRegisterViewModel(returnUrl);

                return(View(viewModel));
            }

            var emailAddress = Regex.Replace(inputModel.EmailAddress, @"\+[^@]+", "");

            if (await _userManager.FindByEmailAsync(emailAddress) != null)
            {
                ModelState.AddModelError(nameof(inputModel.EmailAddress), "This email address has already been registered");

                viewModel = BuildRegisterViewModel(returnUrl);

                return(View(viewModel));
            }

            var user = new StubblUser
            {
                FirstName    = inputModel.FirstName,
                LastName     = inputModel.LastName,
                EmailAddress = emailAddress,
                Username     = inputModel.EmailAddress
            };

            var result = await _userManager.CreateAsync(user, inputModel.Password);

            if (!result.Succeeded)
            {
                return(View("Error"));
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.RouteUrl("ConfirmEmailAddress", new { userId = user.Id, token, returnUrl },
                                           Request.Scheme);
            var email = new ConfirmEmailAddressEmail
                        (
                user.EmailAddress,
                callbackUrl
                        );

            await _emailSender.SendEmailAsync(email, cancellationToken);

            if (_signInManager.Options.SignIn.RequireConfirmedEmail)
            {
                return(RedirectToRoute("EmailAddressConfirmationSent", new { userId = user.Id, returnUrl }));
            }

            await _signInManager.SignInAsync(user, false);

            if (_interactionService.IsValidReturnUrl(returnUrl) || Url.IsLocalUrl(returnUrl))
            {
                return(Redirect(returnUrl));
            }

            return(RedirectToRoute("Home"));
        }
Beispiel #3
0
        public async Task <IActionResult> ExternalLoginCallback([FromForm] ExternalLoginCallbackInputModel inputModel,
                                                                [FromQuery] string returnUrl, [FromQuery] bool isEmailAddressConfirmed, CancellationToken cancellationToken)
        {
            var externalLoginInfo = await _signInManager.GetExternalLoginInfoAsync();

            if (externalLoginInfo == null)
            {
                return(View("Error"));
            }

            if (!ModelState.IsValid)
            {
                var viewModel = BuildExternalLoginCallbackViewModel(inputModel, externalLoginInfo.LoginProvider, returnUrl);

                return(View(viewModel));
            }

            if (await _userManager.FindByEmailAsync(inputModel.EmailAddress) != null)
            {
                ModelState.AddModelError(nameof(inputModel.EmailAddress), "This email address has already been registered");

                var viewModel = BuildExternalLoginCallbackViewModel(inputModel, externalLoginInfo.LoginProvider, returnUrl);

                return(View(viewModel));
            }

            var user = new StubblUser
            {
                EmailAddress          = inputModel.EmailAddress,
                EmailAddressConfirmed = isEmailAddressConfirmed,
                FirstName             = inputModel.FirstName,
                LastName = inputModel.LastName,
                Username = inputModel.EmailAddress
            };

            var result = await _userManager.CreateAsync(user);

            if (!result.Succeeded)
            {
                return(View("Error"));
            }

            result = await _userManager.AddLoginAsync(user, externalLoginInfo);

            if (!result.Succeeded)
            {
                await _userManager.DeleteAsync(user);

                return(View("Error"));
            }

            await _signInManager.UpdateExternalAuthenticationTokensAsync(externalLoginInfo);

            if (!isEmailAddressConfirmed)
            {
                var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var callbackUrl = Url.RouteUrl("ConfirmEmailAddress", new { userId = user.Id, token, returnUrl },
                                               Request.Scheme);
                var email = new ConfirmEmailAddressEmail
                            (
                    user.EmailAddress,
                    callbackUrl
                            );

                await _emailSender.SendEmailAsync(email, cancellationToken);

                if (_signInManager.Options.SignIn.RequireConfirmedEmail)
                {
                    return(RedirectToRoute("EmailAddressConfirmationSent", new { userId = user.Id, returnUrl }));
                }

                return(View("Error"));
            }

            await _signInManager.SignInAsync(user, false);

            if (_interactionService.IsValidReturnUrl(returnUrl))
            {
                return(Redirect(returnUrl));
            }

            return(RedirectToRoute("Home"));
        }
Beispiel #4
0
        public async Task <IActionResult> ChangeEmailAddress([FromForm] ChangeEmailAddressInputModel inputModel, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(View(inputModel));
            }

            var user = await _userManager.GetUserAsync(HttpContext.User);

            if (user == null)
            {
                return(View("Error"));
            }

            if (!await _userManager.IsEmailConfirmedAsync(user))
            {
                return(RedirectToRoute("EmailAddressConfirmationSent", new { userId = user.Id }));
            }

            var emailAddress = Regex.Replace(inputModel.EmailAddress, @"\+[^@]+", "");

            if (string.Equals(emailAddress, user.EmailAddress, StringComparison.InvariantCultureIgnoreCase))
            {
                return(RedirectToRoute("Home"));
            }

            if (await _userManager.FindByEmailAsync(emailAddress) != null)
            {
                ModelState.AddModelError(nameof(inputModel.EmailAddress), "This email address has already been registered");

                return(View(inputModel));
            }

            user.NewEmailAddress = emailAddress;

            var result = await _userManager.UpdateAsync(user);

            if (!result.Succeeded)
            {
                return(View("Error"));
            }

            var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.RouteUrl("ConfirmEmailAddress", new { userId = user.Id, token },
                                           Request.Scheme);
            var email = new ConfirmEmailAddressEmail
                        (
                user.NewEmailAddress,
                callbackUrl
                        );

            await _emailSender.SendEmailAsync(email, cancellationToken);

            if (_signInManager.Options.SignIn.RequireConfirmedEmail)
            {
                return(RedirectToRoute("EmailAddressConfirmationSent", new { userId = user.Id }));
            }

            return(RedirectToRoute("Home"));
        }