1
votes
  1. Exported Qlik Sense certificate using QMC (client.pfx, root.cer, server.pfx).

  2. Imported certificates into IIS web server using MMC. Server and client certificates to store Personal->Certificates, root to store Trusted Root Certification Authorities.

  3. Requested QRS API from ASP.NET controller using QlikClient certificate from store (code below). Tried various user IDs and directories, including INTERNAL/sa_repository, but in all cases got an error "An error occurred while sending the request. The client certificate credentials were not recognized".

Endpoint for test : https://server:4242/qrs/about

I've searched the web but I haven't managed to find what I'm doing wrong, what credentials I should provide.

On the other hand, as I converted exported certificates to separate .key/.crt files (using https://www.markbrilman.nl/2011/08/howto-convert-a-pfx-to-a-seperate-key-crt-file/) and used them in the Postman from web server, it worked without any problem, actually with any UserId in header (i guess it's ignored in that case).

ASP.NET controller:

    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)
        {
            ...
        }
    }
1

1 Answers

0
votes

I ran into this issue on a system we are building.

The problem was that the user did not have rights to the certificate.

Open certificate manager (Start > Manage Computer Certificates) Find the required certificate.

Right-click cert > All Tasks > Manage Private Keys > Add > [Select the appropriate user]

Note: Manage User Certificates does not have the Manage Private Keys option.