Abhijeet
Abhijeet

Reputation: 13916

How to invoke SSL enabled Web Api?

I need to invoke SSL enabled Web Api from a WinForm application. How do I configure HttpClient for this?

These are the pre-requisites I have already completed:

  1. I enabled SSL in Web Api using RequireHttpsAttribute.
  2. I did the necessary certificate import tasks in IIS to make my Web Api work correctly.

Upvotes: 1

Views: 278

Answers (1)

Regfor
Regfor

Reputation: 8101

You can try yo use HttpClinet class with HttpClientHandler.

X509Certificate2 cert;
X509Store store = new X509Store(StoreName.My);

try
{
    store.Open(OpenFlags.ReadOnly);
    cert = store.Certificates.OfType<X509Certificate2>().First(p => p.Thumbprint == "...");
    cert.Dump();
}
finally
{
    if (store != null)
        store.Close();
}   

var clientHandler = new WebRequestHandler();
clientHandler.ClientCertificates.Add(cert);
var client = new HttpClient(clientHandler);

Upvotes: 1

Related Questions