mercredi 1 avril 2015

Windows Phone 8.1. http get request self signed certificate

I would like to connect to a url and authenticate me with a certificate. I've added my certificate to the assets.


I have the following code in the OnLaunched method in App.xaml.cs:



try
{
System.Uri certificateFile = new System.Uri("ms-appx:///Assets/filenme.crt");

Windows.Storage.StorageFile file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(certificateFile);
Windows.Storage.Streams.IBuffer certBlob = await Windows.Storage.FileIO.ReadBufferAsync(file);

Windows.Security.Cryptography.Certificates.Certificate rootCert = new Windows.Security.Cryptography.Certificates.Certificate(certBlob);
rootCert.FriendlyName = "Root";
Windows.Security.Cryptography.Certificates.CertificateStore trustedStore = Windows.Security.Cryptography.Certificates.CertificateStores.TrustedRootCertificationAuthorities;
trustedStore.Add(rootCert);
System.Diagnostics.Debug.WriteLine(rootCert.FriendlyName);
System.Diagnostics.Debug.WriteLine(trustedStore);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Certificate: " + ex.Message);
}


And the following code at my request:



CertificateQuery certQuery = new CertificateQuery();
certQuery.FriendlyName = "Root";
IReadOnlyList<Certificate> certs = await CertificateStores.FindAllAsync(certQuery);

System.Diagnostics.Debug.WriteLine(certs.Count);

HttpBaseProtocolFilter protolFilter = new HttpBaseProtocolFilter();

protolFilter.ClientCertificate = certs[0];
HttpClient client = new HttpClient(protolFilter);
var response = await client.GetAsync(url);

Certificate aCertificate = response.RequestMessage.TransportInformation.ServerCertificate;
IBuffer certBlob = aCertificate.GetCertificateBlob();


response.EnsureSuccessStatusCode();

string content = await response.Content.ReadAsStringAsync();
return content;


The problem is that I get the error that my certificate is OutOfRange at this line:



protolFilter.ClientCertificate = certs[0];


Why is my certificate not in the list of ClientCertificates? Please help me. Thanks


Aucun commentaire:

Enregistrer un commentaire