【问题标题】:How to use ssl certificate in uwp如何在 uwp 中使用 ssl 证书
【发布时间】:2019-12-19 16:10:35
【问题描述】:

我在 uwp 中使用我的测试证书。我不知道哪里出了问题,所以这就是我在这里的原因。

我的步骤:

1、打开Package.appxmanifest,进入Capabilities,查看Shared User Certificates。

2,转到声明,选择证书并添加它。在右侧区域,单击 Add New,在 Store name 字段中,我选择了 Trusted Root Certifacte Authorities(其实我不知道这个字段是什么意思,选择哪个)。在内容字段中,我选择我的 pfx/p12 文件。

3、在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,构建并运行。

我收到错误:

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.

【问题讨论】:

    标签: c# ssl uwp


    【解决方案1】:

    从错误消息中,应用程序似乎没有找到您的证书文件。请按照以下步骤操作:

    1. Solution Explorer 中,右键单击您的client.p12 文件,选择Properties
    2. Build Action 设置为Content

    更新

    可能无法正确识别测试证书。我做了以下尝试:

    1. 生成应用的侧载包,从侧载包中获取.cer证书。
    2. 使用cer证书代替pfx/p12证书,应用可以正常运行

    根据您的使用情况,您想使用证书来发起网络安全请求,但这似乎没有使用Certification 扩展名。这是关于如何发起网络安全请求的document。可以作为参考。

    最好的问候。

    【讨论】:

    • 您好,我已经将其设置为内容,如果更新则复制。我在步骤上方做错了什么吗?谢谢。
    • 虽然我成功获取了数据,但是我还是不明白Import cert to appSend cert via HttpClientHandler的区别。我使用了Send cert via HttpClientHandler,它有效。跟着document,好像和我原来的方法一样。
    • 嗨,很高兴看到你解决了这个问题。引入证书,这个操作本身没有问题,但是你可以不用Certificate扩展,这对于网络安全请求来说不是必须的。
    • 对不起,Certificate 扩展是什么意思,哪部分代码指的是扩展。谢谢。
    • 是你的package.appxmanifest中的Declarations,你在里面使用了Certificate
    【解决方案2】:

    Finalllllllllllllllllllllllllllllly,我解决了。 我导入证书文件的方法是错误的。 原始方法只是将证书文件导入本地应用程序,而不是发送到服务器。 我换了另一种方式,将证书添加到HttpClientHandler

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

    然后在声明中,单击右侧区域的删除按钮。

    然后完成!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-18
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多