コード例 #1
0
        public async Task <IActionResult> RegisterAsync([FromBody] UserRegistrationDto user)
        {
            var existingUser = await _userManager.FindByEmailAsync(user.Email);

            if (existingUser != null)
            {
                // User already exits, return same result as per valid email to avoid user enumeration.
                return(Ok("Email was sent"));
            }

            var newUser = new IdentityUser {
                Email = user.Email, UserName = user.Email
            };
            var identityResult = await _userManager.CreateAsync(newUser, user.Password);

            if (identityResult.Succeeded)
            {
                await _applicationUserManager.CreateApplicationUserAsync(user, newUser.Id);

                string callbackUrl = await GetCallbackUrlAsync(newUser);

                await SendRegistrationEmail(newUser.UserName, user.Email, callbackUrl);

                return(Ok("Email was sent"));
            }

            return(BadRequest(new ActionResponse
            {
                Errors = identityResult.Errors.Select(x => x.Description).ToList(),
                Success = false
            }));
        }