How to translate this cURL Rest API call to C#

imene bak 21 Reputation points
2021-11-14T08:45:18.943+00:00

Hello, can anyone help me to translate this curl command to C# please:
curl -X GET -v "HTTPS: //urlwithparameters" --proxy address: port --concert "authority_cert. CRT" --key "my. key" --cert "certification. Pem"

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,237 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sreeju Nair 11,606 Reputation points
    2021-11-14T09:54:38.057+00:00

    Based on your post, you want to call a REST service with a client certification, in this case you may use HttpClient class to send and receive Http Request/HttpResponse from URIs

    https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=net-5.0

    Now you need to create a .pfx certificate from your key and certificate. There are many tools available for this in the web. Find a reference below

    http://slproweb.com/products/Win32OpenSSL.html

    Now in the C#, you can use the following sample code.

    //load the certificate   
    var certPfx = new X509Certificate2("your pfx filename");  
    var clientHandler = new HttpClientHandler();  
    clientHandler.SslProtocols = SslProtocols.Tls12;  
    clientHandler.ClientCertificateOptions = ClientCertificateOption.Manual;  
    clientHandler.ClientCertificates.Add(certPfx);  
    clientHandler.ServerCertificateCustomValidationCallback += (HttpRequestMessage req, X509Certificate2 cert2, X509Chain chain, SslPolicyErrors err) => { return true; };  
      
    clientHandler.Proxy = new WebProxy("proxy address with port");  
    clientHandler.Credentials = new NetworkCredential("username", "password");  
      
    var client = new HttpClient(clientHandler);  
    client.BaseAddress = new Uri("<url to the service>");  
    var response = await client.GetAsync("relative path to the service with params");  
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. imene bak 21 Reputation points
    2021-11-17T15:16:02.2+00:00

    Thank you Sreejukg,

    Loading of the pfx certificate does not pass and gives me a path error. I'm documenting this problem since two days now and I still haven't been able to get my C# code to work.

    To begin, I have 3 files .pem and .crt and .key to call an API from an Application Pool using C# . I tested it using cURL and it worked, command : Curl -X GET -v URI --proxy PROXY --CaCert CRTPATH --key KEYPATH --cert PEMPATH

    I relied then on the .Net documentation and to your answer to know the steps to call an API. First I started by generating a .pfx file, command :openssl pkcs12 -export -out certificate.pfx -inkey privateKey.key -in certificate.crt -certfile CACert.crt this conversion was don using a password. I also give the pool the necessary permissions to read the pfx certificate from local storage, and enble the user profile loading.

    The problem is happening in the X509Certificate generation phase in my C# code returning an unrecognized path error and I believe that the error does not point the real problem. code :

    var certPfx = new X509Certificate2("my pfx filename");

    noting that I'm using the relative path @"C:\folder\certificate.pfx". I also tried introducing the password I had to use during the certificate conversion, code :

    var certPfx = new X509Certificate2("my pfx filename", password);

    declaring first password as a string. And I tried to change my path to "C:\folder\certificate.pfx". same the error still here.

    information that could be useful, application pool uses the .Net 4.0 framework, and I have not specified a specific dll for my application.

    If you can see the problem or you have any good documentation or links that explains something i missed please help me.

    0 comments No comments