繁体   English   中英

如何在uwp中使用ssl证书

[英]How to use ssl certificate in uwp

我在 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.

在此处输入图片说明 在此处输入图片说明

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

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

更新

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

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

从你的使用情况来看,你是想使用证书发起网络安全请求,但是这个好像没有使用Certification扩展。 这是一个关于如何发起网络安全请求的文档 可以作为参考。

此致。

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

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

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

然后完成!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM