コード例 #1
0
        public async Task ExecuteAsync(VerifyUserCommand command, CancellationToken cancellationToken = default)
        {
            if (command == null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                _logger.LogInformation($"{nameof(RegisterUserCommandHandler)}.{nameof(ExecuteAsync)} was cancelled before execution");
                cancellationToken.ThrowIfCancellationRequested();
            }

            var user = await _userManager.FindByIdAsync(command.Subject);

            var confirmationResult = await _userManager.ConfirmEmailAsync(user, Encoding.UTF8.GetString(Convert.FromBase64String(command.Token)));

            if (!confirmationResult.Succeeded)
            {
                throw new EmailConfirmationException(confirmationResult.Errors.First().Description);
            }

            var addPasswordResult = await _userManager.AddPasswordAsync(user, command.Password);

            if (!addPasswordResult.Succeeded)
            {
                throw new PasswordCreationException(addPasswordResult.Errors.First().Description);
            }

            var aggregate = await _aggregateRepository.GetAsync <User, UserState>(command.Subject, cancellationToken);

            var expectedVersion = aggregate.Version;

            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            var streamId = StreamId.New();

            await _userManager.UpdateAsync(user);

            aggregate.Verify();

            await _signInManager.SignInAsync(user, false);

            aggregate.Login();

            await _aggregateRepository.SaveAsync(aggregate, command, expectedVersion, cancellationToken);
        }