Esempio n. 1
0
        public async Task <Registration.OutModel> Registration([FromBody] Registration.InModel inModel)
        {
            RegistrationValidator validationRules = new RegistrationValidator();
            await validationRules.ValidateAndThrowAsync(inModel);

            return(await _iAccessControlService.Registration(inModel));
        }
        public async Task <Registration.Models.OutModel> Registration(Registration.Models.InModel inModel, CancellationToken cancellationToken = default)
        {
            RegistrationValidator validator = new RegistrationValidator();
            await validator.ValidateAndThrowAsync(inModel);

            var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Username == inModel.UserName ||
                                                                  x.Email == inModel.Email, cancellationToken);

            User newUser;

            if (user != null)
            {
                throw new InvalidOperationException($"User with {user.Email} or {user.Username} already exist.");
            }

            byte[] genSalt = Cryptor.GenerateSalt();
            newUser = new User
            {
                Username     = inModel.UserName,
                Email        = inModel.Email,
                PasswordHash = Cryptor.CalculateHashOfPassword(inModel.PasswordHash)
            };
            _dbContext.Users.Add(newUser);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(new Registration.Models.OutModel {
                IdUser = newUser.Id
            });
        }