Ejemplo n.º 1
0
        public async Task <string> CreateUserAsync(string userName, string name, string password)
        {
            try
            {
                var userFromData = new UserDto
                {
                    Role     = (byte)Role.User,
                    Username = userName,
                    Name     = name,
                    Password = _cryptService.EncodePassword(password)
                };

                var user = await TryGetUserAsync(userName);

                if (user != null)
                {
                    throw new UserServiceException("User already exists!");
                }

                var createdUser = await AddAsync(userFromData);

                string authUser;
                if (createdUser != null)
                {
                    authUser = await AuthenticateAsync(createdUser.Username, createdUser.Password);
                }
                else
                {
                    throw new UserServiceException("User has not been added! Please try again!");
                }

                return(authUser);
            }
            catch (CryptServiceException cryptServiceException)
            {
                throw new BusinessLogicException(cryptServiceException.Message, cryptServiceException);
            }
        }