Quering JSON with Certificate

I recently had a requirement to query an application that requires certificate authentication and replies with JSON.  This example utilizes a previous blog for importing the certificate:  https://blogs.msdn.microsoft.com/metaverse/2017/11/09/capturing-a-pki-certificate/

X509SecurityToken certtoken = GetSecurityToken("a1b2c3");
X509Certificate2 cert = new X509Certificate2(certtoken.Certificate);
string accountName = "JDUser";
string url = "https://server.fqdn/rest/" + accountName;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.PreAuthenticate = true;
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
string responseContents = new StreamReader(responseStream).ReadToEnd();
JavaScriptSerializer JsonConvert = new JavaScriptSerializer();
object xmlresponse = JsonConvert.Deserialize<dynamic>(responseContents);
Dictionary<string, object> keyval = new Dictionary<string, object>();
keyval = (new Dictionary<string, object>((Dictionary<string, object>)(xmlresponse)));
Hashtable hashtable = new Hashtable();
foreach(KeyValuePair<string,object> kvset in keyval)
hashtable.Add(kvset.Key, kvset.Value);
catch (WebException)
// handle as you see fit.


Comments (0)

Skip to main content