Exemplo n.º 1
0
        public string GetToken()
        {
            var publicKey = File.ReadAllText(Filename);
            var pos       = Filename.LastIndexOf('.');
            var accessKey = Filename.Substring(0, pos);
            var payload   = new Dictionary <string, object>()
            {
                { "iss", "MyClient" },
                { "exp", DateTimeOffset.Now.ToUnixTimeSeconds() + 30 },
                { "iat", DateTimeOffset.Now.ToUnixTimeSeconds() },
                { "jti", Guid.NewGuid().ToString() }
            };

            publicKey = publicKey.Replace("\n", "");
            publicKey = publicKey.Replace("\r", "");
            publicKey = publicKey.Replace("-----BEGIN PUBLIC KEY-----", "");
            publicKey = publicKey.Replace("-----END PUBLIC KEY-----", "");
            var publicKeyBytes = Convert.FromBase64String(publicKey);

            MemoryStream stream = new MemoryStream(publicKeyBytes);
            X509EncodedPublicKeyImporter importer = X509EncodedPublicKeyImporter.ImportFromDER(stream);
            CngKey rsaKey = ((RSAPublicKeyImporter)importer).ToCngKey();

            var extraHeaders = new Dictionary <string, object>();

            extraHeaders.Add("kid", accessKey);

            return(JWT.Encode(payload, rsaKey, JweAlgorithm.RSA_OAEP_256, JweEncryption.A128GCM, extraHeaders: extraHeaders));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generates the request token
        /// </summary>
        /// <returns></returns>
        private string generateToken()
        {
            var filename  = key + ".pem";
            var pos       = filename.LastIndexOf('.');
            var accessKey = filename.Substring(0, pos);

            // Get public key
            var publicKey = File.ReadAllText(filename);

            publicKey = publicKey.Replace("\n", "");
            publicKey = publicKey.Replace("\r", "");
            publicKey = publicKey.Replace("-----BEGIN PUBLIC KEY-----", "");
            publicKey = publicKey.Replace("-----END PUBLIC KEY-----", "");
            var publicKeyBytes = Convert.FromBase64String(publicKey);

            // Read and understand the key
            var ms       = new MemoryStream(publicKeyBytes);
            var importer = X509EncodedPublicKeyImporter.ImportFromDER(ms);
            var rsaKey   = ((RSAPublicKeyImporter)importer).ToCngKey();

            // Generate the request
            var payload = new Dictionary <string, object>()
            {
                { "iss", "Sample in .net" },
                { "exp", DateTimeOffset.Now.ToUnixTimeSeconds() + 10 },
                { "iat", DateTimeOffset.Now.ToUnixTimeSeconds() },
                { "jti", Guid.NewGuid().ToString() }
            };

            var headers = new Dictionary <string, object>();

            headers.Add("kid", accessKey);

            // Encrypt it
            return(JWT.Encode(payload, rsaKey, JweAlgorithm.RSA_OAEP_256, JweEncryption.A128GCM, extraHeaders: headers));
        }