Ejemplo n.º 1
0
        public async Task <ApiResponse> Create(RegisterViewModel parameters)
        {
            var user = new ApplicationUser
            {
                UserName = parameters.UserName,
                Email    = parameters.Email
            };

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

            if (!result.Succeeded)
            {
                var msg = string.Join(",", result.Errors.Select(i => i.Description));
                _logger.LogWarning($"Error while creating {user.UserName}: {msg}");
                return(new ApiResponse(Status400BadRequest, msg));
            }
            else
            {
                var claimsResult = _userManager.AddClaimsAsync(user, new Claim[] {
                    new Claim(Policies.IsUser, string.Empty),
                    new Claim(JwtClaimTypes.Name, parameters.UserName),
                    new Claim(JwtClaimTypes.Email, parameters.Email),
                    new Claim(JwtClaimTypes.EmailVerified, "false", ClaimValueTypes.Boolean)
                }).Result;
            }

            var defaultRoleExists = await _roleManager.RoleExistsAsync(DefaultRoleNames.User);

            if (defaultRoleExists)
            {
                await _userManager.AddToRoleAsync(user, DefaultRoleNames.User);
            }

            if (Convert.ToBoolean(_configuration["BlazorBoilerplate:RequireConfirmedEmail"] ?? "false"))
            {
                try
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    string callbackUrl = string.Format("{0}/Account/ConfirmEmail/{1}?token={2}", _configuration["BlazorBoilerplate:ApplicationUrl"], user.Id, token);

                    var email = _emailFactory.BuildNewUserConfirmationEmail(user.UserName, user.Email, callbackUrl, user.Id.ToString(), token);
                    email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));

                    _logger.LogInformation("New user created: {0}", user);
                    await _emailManager.SendEmailAsync(email);
                }
                catch (Exception ex)
                {
                    _logger.LogError($"New user email failed: {ex.GetBaseException().Message}");
                }

                return(new ApiResponse(Status200OK, "Create User Success"));
            }

            try
            {
                var email = _emailFactory.BuildNewUserEmail(user.FullName, user.UserName, user.Email, parameters.Password);
                email.ToAddresses.Add(new EmailAddressDto(user.Email, user.Email));

                _logger.LogInformation("New user created: {0}", user);
                await _emailManager.SendEmailAsync(email);
            }
            catch (Exception ex)
            {
                _logger.LogError($"New user email failed: {ex.GetBaseException().Message}");
            }

            var userViewModel = new UserViewModel
            {
                UserId          = user.Id,
                IsAuthenticated = false,
                UserName        = user.UserName,
                Email           = user.Email,
                FirstName       = user.FirstName,
                LastName        = user.LastName
            };

            if (defaultRoleExists)
            {
                userViewModel.Roles = new List <string> {
                    DefaultRoleNames.User
                }
            }
            ;

            return(new ApiResponse(Status200OK, L["User {0} created", userViewModel.UserName], userViewModel));
        }