Example #1
0
        public TToken DecodeToken(string token)
        {
            RSAParameters rsaParams;

            using (var tr = new StringReader(_keyProvider.Key))
            {
                var pemReader = new PemReader(tr, _passwordFinder);
                if (!(pemReader.ReadObject() is RsaKeyParameters publicKeyParams))
                {
                    throw new Exception("Could not read RSA public key");
                }
                rsaParams = DotNetUtilities.ToRSAParameters(publicKeyParams);
            }

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(rsaParams);
                return(JWT.Decode <TToken>(token, rsa, JwsAlgorithm.RS256));
            }
        }
Example #2
0
        public string CreateToken(TToken data)
        {
            RSAParameters rsaParams;

            using (var tr = new StringReader(_keyProvider.Key))
            {
                var pemReader = new PemReader(tr, _passwordFinder);
                if (!(pemReader.ReadObject() is AsymmetricCipherKeyPair keyPair))
                {
                    throw new Exception("Could not read RSA private key");
                }
                var privateRsaParams = keyPair.Private as RsaPrivateCrtKeyParameters;
                rsaParams = DotNetUtilities.ToRSAParameters(privateRsaParams);
            }

            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportParameters(rsaParams);
                return(JWT.Encode(data, rsa, JwsAlgorithm.RS256));
            }
        }