/// <summary> /// Fetch available public key representation validated by the "kid". /// </summary> /// <param name="kid">The keyId</param> /// <param name="apiDomain">The api domain jwt was obtained, for example us1.gigya.com</param> internal static string FetchPublicKey(string kid, string apiDomain) { var resourceUri = $"https://accounts.{apiDomain}/accounts.getJWTPublicKey?V2=true"; var request = (HttpWebRequest)WebRequest.Create(resourceUri); request.Timeout = 30_000; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.Method = "GET"; request.KeepAlive = false; request.ServicePoint.Expect100Continue = false; GSResponse response; using (var webResponse = (HttpWebResponse)request.GetResponse()) using (var sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) response = new GSResponse(method: request.Method, responseText: sr.ReadToEnd(), logSoFar: null); if (response.GetErrorCode() == 0) { GSArray keys = response.GetArray("keys", null); if (keys == null || keys.Length == 0) { return(null); // Failed to obtain JWK from response data OR data is empty } foreach (object key in keys) { if (key is GSObject) { string jwtKid = ((GSObject)key).GetString("kid", null); if (jwtKid != null && jwtKid == kid) { return(((GSObject)key).ToJsonString()); } } } } return(null); }