示例#1
0
        public async Task <LoginDto> HandleAsync(LogUserLoginRequest command)
        {
            var salt = _db.Users.FirstOrDefault(u => u.Username == command.User.Username)?.PasswordSalt;

            if (salt == null)
            {
                throw new BadRequestException();
            }

            var passwordHash = AuthorizationExtensions.CreatePasswordHash(command.User.Password, salt);

            var registeredUser = _db.Users.Where(u => u.Username == command.User.Username && u.Password == passwordHash).FirstOrDefault();

            if (registeredUser == null)
            {
                throw new UnauthorizedAccessException();
            }

            var currentLogin = _db.Logins.Where(l => l.UserId == registeredUser.Id).OrderByDescending(l => l.ExpirationDate).FirstOrDefault();

            if (currentLogin != null && currentLogin.ExpirationDate.IsValidExpirationDate())
            {
                currentLogin.ExpirationDate = DateTimeExtensions.CreateExpirationDate();
            }
            else
            {
                var entity = new LoginEntity();
                entity.UserId         = registeredUser.Id;
                entity.AuthToken      = Guid.NewGuid().ToString();
                entity.ExpirationDate = DateTimeExtensions.CreateExpirationDate();
                _db.Logins.Add(entity);
            }

            await _db.SaveChangesAsync().ConfigureAwait(false);

            var savedLogin = _db.Logins.Where(l => l.UserId == registeredUser.Id)
                             .OrderByDescending(l => l.ExpirationDate)
                             .Select(u => new { u.Id, u.AuthToken, u.UserId }).FirstOrDefault();

            var loginDto = new LoginDto();

            loginDto.Id        = savedLogin.Id;
            loginDto.AuthToken = savedLogin.AuthToken;
            loginDto.UserId    = savedLogin.UserId;

            return(loginDto);
        }
        public async Task <LoginDto> HandleAsync(RegisterUserRequest request)
        {
            var matchingUsers = _db.Users.Any(u => u.Username == request.User.Username);

            if (matchingUsers)
            {
                throw new BadRequestException();
            }

            var userEntity = Mapper.Map <UserEntity>(request.User);

            var salt         = AuthorizationExtensions.CreateSalt();
            var passwordHash = AuthorizationExtensions.CreatePasswordHash(request.User.Password, salt);

            userEntity.Password     = passwordHash;
            userEntity.PasswordSalt = salt;

            _db.Users.Add(userEntity);

            var loginEntity = new LoginEntity();

            loginEntity.UserId         = userEntity.Id;
            loginEntity.AuthToken      = Guid.NewGuid().ToString();
            loginEntity.ExpirationDate = DateTimeExtensions.CreateExpirationDate();
            _db.Logins.Add(loginEntity);

            await _db.SaveChangesAsync().ConfigureAwait(false);

            var savedLogin = _db.Logins.Where(l => l.UserId == userEntity.Id).OrderByDescending(l => l.ExpirationDate).Select(u => new { u.Id, u.AuthToken }).FirstOrDefault();
            var loginDto   = new LoginDto();

            loginDto.Id        = savedLogin.Id;
            loginDto.AuthToken = savedLogin.AuthToken;

            return(loginDto);
        }