Exemplo n.º 1
0
        private string _generateToken(int id)
        {
            string             token    = null;
            IJWTContainerModel jwtModel = StaticJWTContainer.GetAuthModel(id.ToString());

            _jwtService.SetVariable(jwtModel.SecretKey);
            token = _jwtService.GenerateToken(jwtModel);
            return(token);
        }
Exemplo n.º 2
0
        private string _generateRefreshToken(int id)
        {
            IJWTContainerModel jwtModel = StaticJWTContainer.GetAuthModel(id.ToString());

            jwtModel.SecretKey     = _secretKeyRefreshToken;
            jwtModel.ExpireMinutes = 13;
            _jwtService.SetVariable(jwtModel.SecretKey);
            return(_jwtService.GenerateToken(jwtModel));
        }
Exemplo n.º 3
0
 public JWTAuthAttribute(EUserRole[] roles)
 {
     _roles         = roles;
     _authModel     = StaticJWTContainer.GetAuthModel();
     _jwtService    = new JWTService();
     _isCanAccess   = false;
     _objectService = new ObjectService <UserModel>();
     _jwtService.SetVariable(_authModel.SecretKey);
     _db = new DBCovalmor();
 }
Exemplo n.º 4
0
        public string GenerateToken(IJWTContainerModel model)
        {
            if (model == null || model.Claims == null || model.Claims.Length == 0)
            {
                throw new ArgumentException("Arguments to create token are not valid.");
            }
            _securityKey = new SymmetricSecurityKey(_signKey);
            _credentials = new SigningCredentials(_securityKey, model.SecurityAlgorithm);
            SecurityTokenDescriptor securityTokenDescriptor = new SecurityTokenDescriptor()
            {
                Subject            = new ClaimsIdentity(model.Claims),
                Expires            = DateTime.UtcNow.AddMinutes(Convert.ToInt32(model.ExpireMinutes)),
                SigningCredentials = _credentials
            };

            _securityToken = _handler.CreateToken(securityTokenDescriptor);
            return(_handler.WriteToken(_securityToken));
        }
Exemplo n.º 5
0
        public string GenerateToken(IJWTContainerModel model)
        {
            if (model == null || model.Claims == null || model.Claims.Length == 0 || model.Claims.Any(x => string.IsNullOrEmpty(x.Value)))
            {
                throw new ArgumentNullException("Arguments to create token are not valid.");
            }

            var securityTokenDescriptor = new SecurityTokenDescriptor
            {
                Subject            = new ClaimsIdentity(model.Claims),
                Expires            = DateTime.Now.AddMinutes(Convert.ToInt32(model.ExpireMinutes)),
                SigningCredentials = new SigningCredentials(this.GetSymmetricSecurityKey(), model.SecurityAlgorithm)
            };

            var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
            var securityToken           = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor);
            var token = jwtSecurityTokenHandler.WriteToken(securityToken);

            return(token);
        }