Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public X509Certificate2 LoadQlikCertificate()
- {
- X509Certificate2 certificate = null;
- try
- {
- // Open certification store (MMC)
- X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
- store.Open(OpenFlags.ReadOnly);
- // Get certiface based on the friendly name
- certificate = store.Certificates.Cast<X509Certificate2>().FirstOrDefault(c => c.FriendlyName == "QlikClient");
- // Logging for debugging purposes
- if (certificate != null)
- {
- logger.Log(LogLevel.Warning, $"Certificate: {certificate.FriendlyName} {certificate.GetSerialNumberString()}");
- }
- else
- {
- logger.Log(LogLevel.Warning, $"Certificate: No certificate");
- }
- // Close certification store
- store.Close();
- // Return certificate
- return certificate;
- }
- catch (Exception e)
- {
- ...
- }
- }
- /* Get Qlik API response
- ***********************/
- [HttpGet("getqlikapi")]
- public IActionResult GetQlikAPI()
- {
- // Get Qlik certificate
- var certificate = this.LoadQlikCertificate();
- try
- {
- ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
- // Set server name
- string server = "server";
- // HARDCODED USER AND DIRECTORY FOR TESTING
- string userID = "sa_repository"; // tried also other user ids
- string userDirectory = "INTERNAL";
- // Set Xrfkey header to prevent cross-site request forgery
- string xrfkey = "abcdefg123456789";
- // Create URL to REST endpoint
- string url = $"https://{server}:4242/qrs/about?xrfkey={xrfkey}";
- // The JSON object containing the UserId and UserDirectory
- string body = $"{{ 'UserId': '{userID}', 'UserDirectory': '{userDirectory}', 'Attributes': [] }}";
- // Encode the json object and get the bytes
- byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
- // Create the HTTP Request
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
- // Add the method to authentication the user
- request.ClientCertificates.Add(certificate);
- // POST request will be used
- request.Method = "POST";
- // The request will accept responses in JSON format
- request.Accept = "application/json";
- // 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)
- request.Headers.Add("X-Qlik-Xrfkey", xrfkey);
- request.ContentType = "application/json";
- request.ContentLength = bodyBytes.Length;
- Stream requestStream = request.GetRequestStream();
- requestStream.Write(bodyBytes, 0, bodyBytes.Length);
- requestStream.Close();
- // Make the web request and get response
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream stream = response.GetResponseStream();
- // Return string in response
- //return new OkObjectResult(stream != null ? new StreamReader(stream).ReadToEnd() : string.Empty);
- return new OkObjectResult("test");
- }
- catch (Exception e)
- {
- ...
- }
- }
Add Comment
Please, Sign In to add comment