在Windows Store XAML应用程序中安装客户端证书

6
我想在Windows Store XAML应用程序中使用客户端证书认证。使用makecert,我创建了自签名CA和客户端证书,在IIS/ASP.NET +浏览器(IE10,Chrome等)中进行身份验证工作正常。 现在我想在Windows Store应用程序中使用它,但不确定如何安装证书。我有一个cert.pfx文件,已导入到IE10中。 以下是我用于通过SSL消费HTTP服务的代码。
HttpClientHandler handler = new HttpClientHandler();
handler.ClientCertificateOptions = ClientCertificateOption.Automatic;

HttpClient client = new HttpClient(handler);

我不确定ClientCertificateOption.Automatic和ClientCertificateOption.Manual之间的区别。 当我尝试连接时,证书未被呈现给服务器,我收到401错误。我猜测证书不存在于应用程序证书存储中,因此没有发送任何内容到服务器。那么我该如何安装证书?
我应该使用CertificateEnrollmentManager.ImportPfxDataAsync()方法吗?如果是这样,如何将.pfx转换为“Base64编码的PFX消息”? pfx是否应包含私钥?
或者我应该像这里描述的那样使用Certificates扩展: http://msdn.microsoft.com/en-us/library/windows/apps/hh464981.aspx#certificates_extension_content
1个回答

6
以下代码将加载一个pfx文件并创建一个base64编码的字符串,可供ImportPfxDataAsync方法使用:
StorageFolder packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder certificateFolder = await packageLocation.GetFolderAsync("Certificates");
StorageFile certificate = await certificateFolder.GetFileAsync("YourCert.pfx");

IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(certificate);
string encodedString = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);

这里假设您将证书放在“Certificates”文件夹中。
您可能会想要查看http://www.piotrwalat.net/client-certificate-authentication-in-asp-net-web-api-and-windows-store-apps/,其中详细介绍了在Windows 8应用程序中使用客户端证书与asp.net web api通信的端到端场景。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接