public async Task <AuthorizationSuccessResponse> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(request.Username))
            {
                throw new LogicException(LogicErrorCode.UserNameDoesNotHaveValue, "Username is required to register");
            }

            if (string.IsNullOrWhiteSpace(request.Email))
            {
                throw new LogicException(LogicErrorCode.EmailDoesNotHaveValue, "Email is required to register");
            }

            if (string.IsNullOrWhiteSpace(request.Password))
            {
                throw new LogicException(LogicErrorCode.PasswordDoesNotHaveValue, "Password is required to register");
            }

            if (_identityService.UserWithEmailExists(request.Email))
            {
                throw new LogicException(LogicErrorCode.UserWithSameEmailExist, "Email is not unique. Choose other email");
            }

            if (_identityService.UserWithUsernameExists(request.Username))
            {
                throw new LogicException(LogicErrorCode.UserWithSameUsernameExist, "Username is not unique. Choose other username");
            }

            _identityValidator.ValidateEmail(request.Email);

            _identityValidator.ValidatePassword(request.Password);

            var newUser = new User()
            {
                Email    = request.Email,
                UserName = request.Username
            };

            var createdUser = await _userManager.CreateAsync(newUser, request.Password);

            if (!createdUser.Succeeded)
            {
                throw new LogicException(LogicErrorCode.FailedOnUserCreation, $@"Could not create user with username {request.Username}
                    due to errors: {string.Join(',', createdUser.Errors.Select(x => x.Description))}");
            }

            return(_identityService.GenerateAuthorizationResultForUser(newUser, request.Secret));
        }
        public async Task <Guid> GetOrCreateMailingEmail(string email)
        {
            var mailingEmail = _mailingEmailRepository.GetAll()
                               .FirstOrDefault(me => me.Email.ToLower() == email.ToLower());

            if (mailingEmail != null)
            {
                return(mailingEmail.Id);
            }

            _identityValidator.ValidateEmail(email);

            mailingEmail = new MailingEmail()
            {
                Email = email
            };

            await _mailingEmailRepository.Add(mailingEmail);

            return(mailingEmail.Id);
        }
Beispiel #3
0
        public void ValideteEmail_EmailHasAt_ShouldBeValidated()
        {
            var validatedEmail = "*****@*****.**";

            _identityValidator.ValidateEmail(validatedEmail);
        }