Beispiel #1
0
        public async Task <User> Handle(
            FindUserByLoginAndPasswordQuery query,
            CancellationToken cancellationToken
            )
        {
            cancellationToken.ThrowIfCancellationRequested();

            using (var transaction =
                       await _dbContext.Database.BeginTransactionAsync(IsolationLevel.RepeatableRead, cancellationToken))
            {
                try
                {
                    var user = await _dbContext
                               .Users
                               .IncludeDefault()
                               .AsNoTracking()
                               .SingleOrDefaultAsync(
                        x => EF.Functions.Like(x.Login, query.Login),
                        cancellationToken
                        );

                    if (user != null && !_passwordValidator.Verify(user.PasswordHash, query.Password))
                    {
                        throw new HttpStatusCodeException(
                                  422,
                                  "Wrong password.",
                                  "The user found, but the password was wrong."
                                  );
                    }

                    transaction.Commit();
                    return(user);
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }