public async Task <AuthenticationResult> RegisterAsync(UserRegistrationRequest request)
        {
            var existingUser = await _userManager.FindByEmailAsync(request.Email);

            if (existingUser != null)
            {
                return(new AuthenticationResult
                {
                    Errors = new[] { "User with this email address already exists" }
                });
            }

            var newUserId = Guid.NewGuid();
            var newUser   = new CustomUser
            {
                Id             = newUserId.ToString(),
                Email          = request.Email,
                UserName       = request.UserName,
                EmailConfirmed = true
            };

            var createdUser = await _userManager.CreateAsync(newUser, request.Password);

            if (!createdUser.Succeeded)
            {
                return(new AuthenticationResult
                {
                    Errors = createdUser.Errors.Select(x => x.Description)
                });
            }

            return(await GenerateAuthenticationResultForUserAsync(newUser));
        }
Beispiel #2
0
        public async Task <IActionResult> RegisterConfirmation(string email, string returnUrl = null)
        {
            if (email == null)
            {
                return(RedirectToAction(nameof(Register)));
            }

            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                return(NotFound($"Unable to load user with email '{email}'."));
            }
            var registerConfirmationViewModel = new RegisterConfirmationViewModel()
            {
                Email = email,
                DisplayConfirmAccountLink = true//set to true if you want to display page for registerconfirmation
            };

            //TODO: add email sender and confirmation https://app.sendgrid.com/

            if (registerConfirmationViewModel.DisplayConfirmAccountLink)
            {
                var userId = await _userManager.GetUserIdAsync(user);

                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                registerConfirmationViewModel.EmailConfirmationUrl = Url.Page(
                    "/Account/ConfirmEmail",
                    pageHandler: null,
                    values: new { area = "Identity", userId = userId, code = code, returnUrl = returnUrl },
                    protocol: Request.Scheme);
            }

            return(View(registerConfirmationViewModel));
        }