コード例 #1
0
        public ClaimsPrincipal GetPrincipal(string token)
        {
            try
            {
                var appKey = "";
                using (var dbContext = new PromoDbContext())
                {
                    appKey = dbContext.ClientSecrets.FirstOrDefault(cs => cs.AppValue == token)?.AppKey;
                }
                var tokenHandler = new JwtSecurityTokenHandler();

                if (!(tokenHandler.ReadToken(token) is JwtSecurityToken jwtToken))
                {
                    return(null);
                }

                var symmetricKey = Convert.FromBase64String(appKey);

                var validationParameters = new TokenValidationParameters()
                {
                    RequireExpirationTime = false,
                    ValidateIssuer        = false,
                    ValidateLifetime      = false,
                    ValidateAudience      = false,
                    IssuerSigningKey      = new SymmetricSecurityKey(symmetricKey)
                };

                var principal = tokenHandler.ValidateToken(token, validationParameters, out _);
                return(principal);
            }

            catch (Exception exce)
            {
                string error = exce.Message;
                return(null);
            }
        }
コード例 #2
0
 public PromoController(PromoDbContext context)
 {
     _context = context;
 }
コード例 #3
0
 public ClientSecretController(PromoDbContext context, IJwtManager jwtManager)
 {
     _context    = context;
     _jwtManager = jwtManager;
 }