예제 #1
0
            public async Task <Unit> Handle(RegisterCommand request, CancellationToken cancellationToken)
            {
                if (await _context.Users.Where(x => x.Email.ToLower() == request.Email.ToLower()).AnyAsync(cancellationToken: cancellationToken))
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Email = "Email already exists" });
                }
                var user = new AppUser
                {
                    DisplayName = request.DisplayName,
                    Email       = request.Email,
                    UserName    = request.Email,
                    Gender      = request.Gender,
                    Birthday    = request.Birthday,
                    IsActive    = true,
                };

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

                if (result.Succeeded)
                {
                    var token = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);

                    var passwordResetLink = urlHelper.Action("ConfirmEmail", "ConfirmEmail",
                                                             new { user = user.Id, token }, _contextAccessor.HttpContext.Request.Scheme);
                    var email = new EmailDto
                    {
                        SenderAddress   = _configuration["SenderAddress"],
                        ReceiverAddress = "*****@*****.**",
                        Subject         = "Confirm Email.",
                        TextBody        = passwordResetLink,
                    };
                    await _emailService.SendEmail(email);

                    return(Unit.Value);
                }

                var errors = result.Errors.Aggregate("", (current, error) => current + error.Description);

                throw new Exception(errors);
            }