Exemplo n.º 1
0
        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));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> RegisterConfirmed(/*[Bind("Email,Password")]*/ RegisterInputModel Input)
        {
            var returnUrl = Input.ReturnUrl ?? Url.Content("~/");

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new CustomUser
                {
                    UserName    = Input.UserName,
                    Email       = Input.Email,
                    FirstName   = Input.FirstName,
                    LastName    = Input.LastName,
                    PhoneNumber = Input.PhoneNumber,
                    Country     = Input.Country,
                    City        = Input.City,
                    Address     = Input.Address
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = 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", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

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

            return(View(Input));
        }
Exemplo n.º 3
0
        private async Task <string> EnsureUser(
            ICustomUserManager userManager,
            CustomUser newUser,
            string Password)
        {
            var user = await userManager.FindByNameAsync(newUser.UserName);

            if (user == null)
            {
                user = newUser;
                userManager.CreateAsync(user, Password).Wait();
            }
            return(user.Id);
        }