Пример #1
0
        public async Task <AuthenticationInfo> AuthenticateAsync(AuthenticationModel model)
        {
            model.RejectInvalid();

            var user = await _userRepository.AsReadOnly().Include(_ => _.Role).SingleOrDefaultAsync(_ => _.UserName == model.UserName);

            user.RejectNotFound();

            var passwordHash = _cryptographyService.CreateHash(model.Password, user.PasswordSalt);

            if (passwordHash != user.PasswordHash)
            {
                throw new InvalidModelException();
            }

            TokenData tokenData = null;

            try
            {
                tokenData = Mapper.Map <Entities.User, TokenData>(user);
            }
            catch (Exception ex)
            {
                user.RejectInvalid();
            }

            var tokenTask        = _tokenProvider.CreateTokenAsync(tokenData);
            var refreshTokenTask = _tokenProvider.CreateRefreshTokenAsync(tokenData);

            return(new AuthenticationInfo()
            {
                UserName = user.UserName,
                DisplayName = user.DisplayName,
                Token = await tokenTask,
                RefreshToken = await refreshTokenTask
            });
        }