【问题标题】:The Right way to import server certificate (+ chain) in UWPUWP中导入服务器证书(+链)的正确方法
【发布时间】:2018-11-20 16:53:08
【问题描述】:

我正在尝试导入随后连接的服务器证书。

  1. 我的第一个障碍是导入证书的正确方法是什么 在 UWP 中,[注意:打包 .pfx 违反商店政策]
  2. 导入服务器证书后,如何导入/获取签署服务器证书的根 CA 的证书?

我目前正在使用下面的代码,但我仍然有上述问题?

    public async Task InsertCert()
    {
        StorageFile pfxfile = await ApplicationData.Current.LocalFolder.GetFileAsync("ms-appx:///myfile.pfx");
        var buffer = await FileIO.ReadBufferAsync(pfxfile);

        string certificateData = CryptographicBuffer.EncodeToBase64String(buffer);

        string password = "";

        await CertificateEnrollmentManager.ImportPfxDataAsync(
                certificateData,
                password,
                ExportOption.NotExportable,
                KeyProtectionLevel.NoConsent,
                InstallOptions.None,
                "Client Certificate");
    }

【问题讨论】:

    标签: c# uwp certificate pfx


    【解决方案1】:

    我检查了你的代码,但有些地方我不明白。

    例如,您使用此行代码在本地文件夹中获取“.pfx”文件。

    StorageFile pfxfile = await ApplicationData.Current.LocalFolder.GetFileAsync("ms-appx:///myfile.pfx");

    ms-appx:/// 是您的应用程序包。 ApplicationData.Current.LocalFolder 是您的应用程序数据文件夹,它等于ms-appdata:///local/。它们是不同的东西。

    在你的情况下,如果'.pfx'文件在本地文件夹的根目录下,你可以直接使用await ApplicationData.Current.LocalFolder.GetFileAsync("myfile.pfx")来获取它。

    然后,让我们回到您的“导入/获取证书”问题。我看到您正在使用CertificateEnrollmentManager.ImportPfxDataAsync 在应用容器商店中安装“.pfx”证书。没错。

    成功安装证书后,您可以通过调用Windows.Security.Cryptography.Certificates.CertificateStores.FindAllAsync(certQuery) 获取它。

    根据您在 'ImportPfxDataAsync' 方法中指定的 FriendlyName,您可以创建 CertificateQuery 作为 CertificateStores.FindAllAsync 方法参数。

    Windows.Security.Cryptography.Certificates.CertificateQuery certQuery = new Windows.Security.Cryptography.Certificates.CertificateQuery();
    certQuery.FriendlyName = "Client Certificate";    // This is the friendly name of the certificate that was just installed.
    IReadOnlyList<Windows.Security.Cryptography.Certificates.Certificate> certs = await Windows.Security.Cryptography.Certificates.CertificateStores.FindAllAsync(certQuery);
    foreach (Certificate cert in certs)
    {
        Debug.WriteLine($"FriendlyName: {cert.FriendlyName},Subject: {cert.Subject}, Serial Number: {CryptographicBuffer.EncodeToHexString(CryptographicBuffer.CreateFromByteArray(cert.SerialNumber))}");
    }
    

    找到证书后,您可以使用它与您的服务器进行通信。

    例如,

    您可以使用 Windows.Web.Http.HttpClient 类以编程方式附加已安装的客户端证书。

    Windows.Web.Http.Filters.HttpBaseProtocolFilter filter= new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
    filter.ClientCertificate = [your certificate];
    Windows.Web.Http.HttpClient Client = new Windows.Web.Http.HttpClient(filter);
    await Client.GetAsync(...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-01
      • 2014-06-11
      • 2018-03-24
      • 2018-04-27
      相关资源
      最近更新 更多