【发布时间】:2014-08-13 13:30:50
【问题描述】:
我正在尝试在我的应用程序中实现 Https 客户端身份验证,但找不到任何有关如何执行此操作的文档。
查看我想出的 MSDN 文档
// Certificate file in DER format (.cer or .p7b)
string CountriesFile = @"Assets\https-client.keystore.cer";
StorageFolder InstallationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFile file = await InstallationFolder.GetFileAsync(CountriesFile);
// Read the file into a buffer
IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
// Create the Certificate object
Certificate ClientCert = new Certificate(buffer);
HttpBaseProtocolFilter aHBPF = new HttpBaseProtocolFilter();
aHBPF.ClientCertificate = ClientCert;
// Create our http client and send the request.
HttpClient httpClient = new HttpClient(aHBPF);
HttpResponseMessage response = await httpClient.SendRequestAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead).AsTask(cts.Token);
我将这段代码放在一起查看HttpClient、HttpBaseProtocolFilter 和Certificate 的文档。假设我应该拥有所需格式的证书并将文件读入Certificate 类。
上面的代码不起作用并抛出这个错误
An exception of type 'System.ArgumentException' occurred in MyLib.DLL but was not handled in user code
WinRT information: The certificate specified is missing the required private key information.
我已经测试了我的服务器设置,它通过浏览器与客户端身份验证一起工作,这让我得出了两个可能的结论。
- 证书文件格式错误(虽然我希望在构造
Certificate类时会引发异常)。 - 这不是故意的!
有人知道应该怎么做吗?
【问题讨论】:
-
关于 HRESULT 0x80072F7D,请查看this question 和解决方案。
标签: c# ssl windows-store-apps authentication