Example #1
0
        public async Task <IActionResult> RegisterConfirmation(string email, string returnUrl = null)
        {
            if (email == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var user = await UserEmailStore.FindByEmailAsync(KeyNormalizer.NormalizeEmail(email), CancellationToken.None);

            if (user == null)
            {
                return(NotFound($"Unable to load user with email '{email}'."));
            }

            ViewData["Email"] = email;

            // TODO : remove this code that confirms the account
            var displayConfirmAccountLink = true;

            ViewData["DisplayConfirmAccountLink"] = displayConfirmAccountLink;

            if (displayConfirmAccountLink)
            {
                var userId = await UserManager.GetUserIdAsync(user);

                var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

                var emailConfirmationUrl = Url.Action
                                           (
                    "ConfirmEmail",
                    "Account",
                    new { userId, code, returnUrl },
                    protocol: Request.Scheme
                                           );

                ViewData["EmailConfirmationUrl"] = emailConfirmationUrl;
            }

            return(View("RegisterConfirmation"));
        }
        public async Task <IActionResult> Register(RegisterViewModel input)
        {
            if (ModelState.IsValid)
            {
                var id = CryptoRandom.CreateUniqueId(format: CryptoRandom.OutputFormat.Hex);

                var normalizedEmail = KeyNormalizer.NormalizeEmail(input.Email);

                var user = await UserStore.FindByNameAsync(normalizedEmail, CancellationToken.None) ?? new User
                {
                    Id              = id,
                    UserName        = input.Email,
                    Email           = input.Email,
                    NormalizedEmail = normalizedEmail
                };

                var result = default(IdentityResult);

                if (user.Id == id)
                {
                    result = await UserManager.CreateAsync(user, input.Password);
                }
                else
                {
                    result = IdentityResult.Failed(new IdentityError
                    {
                        Code        = "USER_NOT_CREATED",
                        Description = "Please login with your existing account; user already exists"
                    });
                }

                if (result.Succeeded)
                {
                    var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));

                    var callbackUrl = Url.Page
                                      (
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { userId = user.Id, code = code, returnUrl = input.ReturnUrl },
                        protocol: Request.Scheme
                                      );

                    await EmailSender.SendEmailAsync(input.Email, "Confirm your email", $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (UserManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToAction("RegisterConfirmation", "Account", new { email = input.Email, returnUrl = input.ReturnUrl }));
                    }
                    else
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(input.ReturnUrl));
                    }
                }

                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            return(View(input));
        }