Example #1
0
        public async Task <SessionDto> AddNewSessionAsync(string nickname, string salt)
        {
            string token     = SessionsService.GenerateNewSessionToken();
            string tokenHash = await HashingService.GetHashAsync(token, salt, HashingType.Weak).ConfigureAwait(false);

            this.UnitOfWork.SessionsRepository.Add(new Session()
            {
                Token    = tokenHash,
                Nickname = nickname,
                From     = DateTime.Now
            });

            return(new SessionDto()
            {
                Nickname = nickname,
                Token = token
            });
        }
Example #2
0
        public async Task <SessionDto> CheckAndUpdateSessionAsync(SessionDto session)
        {
            var user = await this.UnitOfWork.UsersRepository.GetUniqueAsync(e => e.Nickname == session.Nickname).ConfigureAwait(false);

            if (user == null)
            {
                throw new SessionException("You should re-enter your credentials.");
            }

            var tokenHash = await HashingService.GetHashAsync(session.Token, user.Salt, HashingType.Weak).ConfigureAwait(false);

            var sessionObj = await this.UnitOfWork.SessionsRepository.GetUniqueAsync(e => e.Token == tokenHash).ConfigureAwait(false);

            if (sessionObj == null)
            {
                throw new SessionException("You should re-enter your credentials.");
            }

            if (sessionObj.Nickname != session.Nickname)
            {
                throw new SessionException("You should re-enter your credentials.");
            }

            string newToken = SessionsService.GenerateNewSessionToken();

            sessionObj.Token = await HashingService.GetHashAsync(newToken, user.Salt, HashingType.Weak).ConfigureAwait(false);

            sessionObj.From = DateTime.Now;
            await this.UnitOfWork.CommitAsync().ConfigureAwait(false);

            return(new SessionDto
            {
                Nickname = session.Nickname,
                Token = newToken
            });
        }