Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing client certificates in Windows Store XAML apps

I would like to use client certificate authentication in Windows Store XAML app. Using makecert i have created a self-signed CA and client certificates, the authentication works in IIS/ASP.NET + browser (IE10,Chrome, etc.) fine. Now I wanted to use it in Windows Store app, but am unsure on how to actually install the certificate. I have a cert.pfx file that i imported to IE10. Here is the code I use to consume HTTP service over SSL.

HttpClientHandler handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;

HttpClient client = new HttpClient(handler);

Not sure whats the difference between ClientCertificateOption.Automatic and ClientCertificateOption.Manual. When I try to connect the certificate is not being presented to the server and i get 401 error I am guessing that the certificate is not present in app cert store and thus nothing is being sent to the server. How do I install the cert then?

Should I use CertificateEnrollmentManager.ImportPfxDataAsync() method? if so how can i convert .pfx to 'Base64-encoded PFX message' Should pfx contain private key?

Or maybe I should use Certificates extension as described here: http://msdn.microsoft.com/en-us/library/windows/apps/hh464981.aspx#certificates_extension_content

like image 831
thallium Avatar asked Oct 24 '12 07:10

thallium


1 Answers

The following code will load a pfx file and create a base64 encoded string that can be used by ImportPfxDataAsync method:

StorageFolder packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder certificateFolder = await packageLocation.GetFolderAsync("Certificates");
StorageFile certificate = await certificateFolder.GetFileAsync("YourCert.pfx");

IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(certificate);
string encodedString = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);

This assumes that you put your certificate in 'Certificates' folder.

You may want to have a look at http://www.piotrwalat.net/client-certificate-authentication-in-asp-net-web-api-and-windows-store-apps/ this walks through end-to-end scenario of using client certificates in windows 8 app to communicate with asp.net web api.

like image 87
Piotr Walat Avatar answered Oct 21 '22 19:10

Piotr Walat