在 UWP 中导入服务器证书(+链)的正确方法
The Right way to import server certificate (+ chain) in UWP
我正在尝试导入我随后要连接的服务器证书。
- 我的第一个障碍是导入证书的正确方法是什么
在 UWP 中,[注意:打包 .pfx 违反商店政策]
- 导入服务器证书后,如何import/obtain签署服务器证书的根 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");
}
我检查了你的代码,但有些地方我不明白。
例如,您使用此行代码在本地文件夹中获取“.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")
来获取它。
那么,让我们回到您的 'import/obtain the certificates' 问题。我看到您正在使用 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 class 以编程方式附加已安装的客户端证书。
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(...);
我正在尝试导入我随后要连接的服务器证书。
- 我的第一个障碍是导入证书的正确方法是什么 在 UWP 中,[注意:打包 .pfx 违反商店政策]
- 导入服务器证书后,如何import/obtain签署服务器证书的根 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");
}
我检查了你的代码,但有些地方我不明白。
例如,您使用此行代码在本地文件夹中获取“.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")
来获取它。
那么,让我们回到您的 'import/obtain the certificates' 问题。我看到您正在使用 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 class 以编程方式附加已安装的客户端证书。
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(...);