我检查了你的代码,但有些地方我不明白。
例如,您使用此行代码在本地文件夹中获取“.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(...);