Example #1
0
        private static string GenerateAuthorizationToken_old(JwtClaims jwtClaims, string appSecret)
        {
            if (jwtClaims == null)
            {
                throw new ArgumentNullException(nameof(jwtClaims));
            }

            if (string.IsNullOrWhiteSpace(appSecret))
            {
                throw new ArgumentException(nameof(appSecret));
            }
            var payload = new Dictionary <string, object>()
            {
                { "exp", 1610241683 },

                { "iat", 1610239883 },
                { "iss", "http://cylance.com" },
                { "sub", "d4146a43-6826-40f3-b136-67de6f996c71" },
                { "tid", "ba50714d-f006-47db-9420-fcc0b00666e3" },
                { "jti", "997ebe57-5425-408c-abfb-a20aad2738ca" }
            };
            var algorithm  = new HMACSHA256Algorithm();
            var serializer = new JsonNetSerializer();
            var encoder    = new JwtBase64UrlEncoder();
            var jwt        = new JwtEncoder(algorithm, serializer, encoder);

            return(jwt.Encode(payload, appSecret));
        }
Example #2
0
        private static string GenerateAuthorizationToken(JwtClaims jwtClaims, string appSecret)
        {
            if (jwtClaims == null)
            {
                throw new ArgumentNullException(nameof(jwtClaims));
            }

            if (string.IsNullOrWhiteSpace(appSecret))
            {
                throw new ArgumentException(nameof(appSecret));
            }

            var algorithm  = new HMACSHA256Algorithm();
            var serializer = new JsonNetSerializer();
            var encoder    = new JwtBase64UrlEncoder();
            var jwt        = new JwtEncoder(algorithm, serializer, encoder);

            return(jwt.Encode(jwtClaims, appSecret));
        }
Example #3
0
        static async Task Main(string[] args)
        {
            {
                const string tenantId  = "ba50714d-f006-47db-9420-fcc0b00666e3";
                const string appId     = "d4146a43-6826-40f3-b136-67de6f996c71";
                const string appSecret = "c8710d12-083c-4c4a-98ff-0b3e5be18247";

                var utcNow    = DateTimeOffset.UtcNow;
                var jwtClaims = new JwtClaims
                {
                    iss = "http://cylance.com",
                    jti = Guid.NewGuid().ToString(),
                    iat = utcNow.ToUnixTimeSeconds(),
                    exp = (utcNow).AddSeconds(1800).ToUnixTimeSeconds(),
                    tid = tenantId,
                    sub = appId
                };

                try
                {
                    // var authToken = GenerateAuthorizationToken(jwtClaims, appSecret);
                    var authToken = GenerateAuthorizationToken(appSecret);
                    //Console.WriteLine($"\n[Authorization Token]\n{authToken}\n");

                    await GenerateAccessToken(authToken);

                    //Console.WriteLine($"\n[Access Token]\n{accessToken}\n");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

                var resp = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(jsonResponse, Encoding.UTF8, "application/json")
                };
                Console.WriteLine(resp.StatusCode);
                Console.WriteLine(resp.Headers);
                Console.WriteLine(resp.RequestMessage);
                Console.WriteLine(resp.Content);
            }
        }