Ejemplo n.º 1
0
        public static string EncryptString(string text, X509Certificate2 cert, bool base64UrlEncoding)
        {
            var bytes          = Encoding.UTF8.GetBytes(text);
            var encrypyedBytes = EncryptBytes(bytes, cert);

            return(base64UrlEncoding ? Base64UrlEncoder.ToBase64String(encrypyedBytes) : Convert.ToBase64String(encrypyedBytes));
        }
Ejemplo n.º 2
0
        public static string GenerateSignatureString(byte[] bytes, AsymmetricAlgorithm asymmetricAlgorithm, XmlSignatureAlgorithmType signatureAlgorithm, bool base64UrlEncoding)
        {
            var signedBytes = GenerateSignatureBytes(bytes, asymmetricAlgorithm, signatureAlgorithm);
            var signedText  = base64UrlEncoding ? Base64UrlEncoder.ToBase64String(signedBytes) : Convert.ToBase64String(signedBytes);

            return(signedText);
        }
Ejemplo n.º 3
0
        private static JwtKey[] GetKeys(X509Certificate2[] certs)
        {
            var keys = new List <JwtKey>();

            foreach (var cert in certs)
            {
                var rsa = cert.GetRSAPublicKey();
                if (rsa == null)
                {
                    throw new IdentityProviderException("X509 must be RSA");
                }

                var parameters = rsa.ExportParameters(false);
                var publicKey  = cert.Export(X509ContentType.Cert);
                var certString = Convert.ToBase64String(publicKey);
                var key        = new JwtKey()
                {
                    KeyID            = cert.Thumbprint,
                    Use              = "sig",
                    KeyType          = "RSA",
                    X509Thumbprint   = cert.Thumbprint, //same as KeyID
                    Exponent         = Base64UrlEncoder.ToBase64String(parameters.Exponent),
                    Modulus          = Base64UrlEncoder.ToBase64String(parameters.Modulus),
                    X509Certificates = new string[]
                    {
                        certString
                    }
                };
                keys.Add(key);
            }
            return(keys.ToArray());
        }
Ejemplo n.º 4
0
        public HttpAuthHeaders BuildAuthHeaders()
        {
            var authCookieData          = "I can access this";
            var authCookieDataBytes     = Encoding.UTF8.GetBytes(authCookieData);
            var authCookieDataEncrypted = SymmetricEncryptor.Encrypt(encryptionAlgorithm, encryptionKey, authCookieDataBytes);
            var authCookieDataEncoded   = Base64UrlEncoder.ToBase64String(authCookieDataEncrypted);

            var cookies = new Dictionary <string, string>
            {
                { cookieName, authCookieDataEncoded }
            };

            var cookieHeaderValue = CookieParser.CookiesToString(cookies);
            var headers           = new HttpAuthHeaders
            {
                { cookieHeader, new List <string>()
                  {
                      cookieHeaderValue
                  } }
            };

            return(headers);
        }