我正在尝试导入服务器证书,该证书将在以后连接。
我目前正在使用下面的代码,但仍然有上面的问题?
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");
}
答案 0 :(得分:0)
我检查了您的代码,但有一些我不明白的地方。
例如,您使用此行代码在本地文件夹中获取“ .pfx”文件。
StorageFile pfxfile =等待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(...);