Guest User

Untitled

a guest
Dec 14th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. public X509Certificate2 LoadQlikCertificate()
  2. {
  3. X509Certificate2 certificate = null;
  4.  
  5. try
  6. {
  7. // Open certification store (MMC)
  8. X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
  9. store.Open(OpenFlags.ReadOnly);
  10.  
  11. // Get certiface based on the friendly name
  12. certificate = store.Certificates.Cast<X509Certificate2>().FirstOrDefault(c => c.FriendlyName == "QlikClient");
  13.  
  14. // Logging for debugging purposes
  15. if (certificate != null)
  16. {
  17. logger.Log(LogLevel.Warning, $"Certificate: {certificate.FriendlyName} {certificate.GetSerialNumberString()}");
  18. }
  19. else
  20. {
  21. logger.Log(LogLevel.Warning, $"Certificate: No certificate");
  22. }
  23.  
  24. // Close certification store
  25. store.Close();
  26.  
  27. // Return certificate
  28. return certificate;
  29. }
  30. catch (Exception e)
  31. {
  32. ...
  33. }
  34. }
  35.  
  36.  
  37. /* Get Qlik API response
  38. ***********************/
  39. [HttpGet("getqlikapi")]
  40. public IActionResult GetQlikAPI()
  41. {
  42. // Get Qlik certificate
  43. var certificate = this.LoadQlikCertificate();
  44.  
  45. try
  46. {
  47. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  48.  
  49. // Set server name
  50. string server = "server";
  51. // HARDCODED USER AND DIRECTORY FOR TESTING
  52. string userID = "sa_repository"; // tried also other user ids
  53. string userDirectory = "INTERNAL";
  54. // Set Xrfkey header to prevent cross-site request forgery
  55. string xrfkey = "abcdefg123456789";
  56.  
  57. // Create URL to REST endpoint
  58. string url = $"https://{server}:4242/qrs/about?xrfkey={xrfkey}";
  59.  
  60. // The JSON object containing the UserId and UserDirectory
  61. string body = $"{{ 'UserId': '{userID}', 'UserDirectory': '{userDirectory}', 'Attributes': [] }}";
  62. // Encode the json object and get the bytes
  63. byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
  64.  
  65. // Create the HTTP Request
  66. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  67. // Add the method to authentication the user
  68. request.ClientCertificates.Add(certificate);
  69. // POST request will be used
  70. request.Method = "POST";
  71. // The request will accept responses in JSON format
  72. request.Accept = "application/json";
  73. // A header is added to validate that this request contains a valid cross-site scripting key (the same key as the one used in the url)
  74. request.Headers.Add("X-Qlik-Xrfkey", xrfkey);
  75. request.ContentType = "application/json";
  76. request.ContentLength = bodyBytes.Length;
  77.  
  78. Stream requestStream = request.GetRequestStream();
  79. requestStream.Write(bodyBytes, 0, bodyBytes.Length);
  80. requestStream.Close();
  81.  
  82. // Make the web request and get response
  83. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  84. Stream stream = response.GetResponseStream();
  85.  
  86. // Return string in response
  87. //return new OkObjectResult(stream != null ? new StreamReader(stream).ReadToEnd() : string.Empty);
  88. return new OkObjectResult("test");
  89. }
  90. catch (Exception e)
  91. {
  92. ...
  93. }
  94. }
Add Comment
Please, Sign In to add comment