How to use ssl certificate in uwp

0

I using my test certificate in uwp. I'm not sure where is wrong, so that's why I'm here.

My steps:

1, open Package.appxmanifest, go to Capabilities, check Shared User Certificates.

2, go to Declarations, select Certificates and add it. On the right area, Click Add New, in Store name field, I selected Trusted Root Certifacte Authorities(In fact, I dont know this field mean, and which one to choose). In Content field, I select my pfx/p12 file.

3, In mainpage.xaml.cs,

StorageFile certificateFile = await Package.Current.InstalledLocation.GetFileAsync(@"client.p12");
        IBuffer certificateBuffer = await FileIO.ReadBufferAsync(certificateFile);
        string encodedCertificate = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(certificateBuffer);
        await CertificateEnrollmentManager.ImportPfxDataAsync(encodedCertificate, "000000", ExportOption.NotExportable, KeyProtectionLevel.NoConsent, InstallOptions.None, "Client Certificate");

        var handler = new HttpClientHandler();
        handler.ClientCertificateOptions = ClientCertificateOption.Manual;
        handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls12 | System.Security.Authentication.SslProtocols.Tls11 | System.Security.Authentication.SslProtocols.Tls;
        handler.ServerCertificateCustomValidationCallback =
            (httpRequestMessage, cert, cetChain, policyErrors) =>
            {
                return true;
            };

        HttpClient client = new HttpClient(handler);
        //HttpResponseMessage response = await client.GetAsync("https://test.client.ssl/");
        HttpResponseMessage response = await client.GetAsync("https://192.168.101.99/");
        response.EnsureSuccessStatusCode();
        string temp = await response.Content.ReadAsStringAsync();

4, build & run.

I got error:

Severity Code Description Project File Line Suppression State
Error DEP0700: Registration of the app failed. [0x80073CF6] AppxManifest.xml (41,10): 
Error 0x80092009: Unable to register b17011a8-22d6-4a6a-bdb9-4a42390c9639_1.0.0.0_x86__701p3ryg2e8g6 package, 
because trying to open and evaluate the client. When adding a p12 certificate to the root store, 
I encountered the following error: The requested object could not be found.

enter image description here enter image description here

c#
ssl
uwp
asked on Stack Overflow Dec 19, 2019 by Vincent

2 Answers

1

From the error message, the application seems does not find your certificate file. Please follow the steps below:

  1. In Solution Explorer, right-click your client.p12 file, choose the Properties
  2. Set Build Action to Content.

Update

The test certificate may not be recognized correctly. I made the following attempt:

  1. Generate the side load package of the application, and get the .cer certificate from the side loading package.
  2. Use cer certificate instead of pfx/p12 certificate, the app can run normally

From your usage, you want to use the certificate to initiate a network security request, but this does not seem to use the Certification extension. This is a document on how to initiate a network security request. It can be used as a reference.

Best regards.

answered on Stack Overflow Dec 19, 2019 by Richard Zhang - MSFT • edited Dec 19, 2019 by Richard Zhang - MSFT
0

Finalllllllllllllllllllllllllllllly, I worked it out. The method I import cert file is wrong. The original method just import cert file to local app, but not send to server. I changed another way, add cert to HttpClientHandler.

X509Certificate2 cer = new X509Certificate2(File.ReadAllBytes("client.pfx"), "000000");
handler.ClientCertificates.Add(cer);

Then in Declarations, click the remove button at the right area.

Then Done!!!

answered on Stack Overflow Dec 19, 2019 by Vincent • edited Dec 19, 2019 by Vincent

User contributions licensed under CC BY-SA 3.0