使用Windows RT的客户端证书(Windows 8.1/Windows phone 8.1)

本文关键字:Windows phone 证书 RT 客户端 使用 | 更新日期: 2023-09-27 18:21:16

我正在尝试windows 8.1和windows phone 8.1的一个新功能,即证书存储和在服务器端使用客户端证书进行客户端身份验证的可能性。然而,我在这个功能上遇到了问题。

我有一个基本的测试WCF服务,它运行在IIS express上。IIS express配置为支持SSL和客户端证书。在IIS的配置文件(configurationhost.config)中,我设置了以下内容:

<access sslFlags="SslRequireCert" /> (tried also SslNegotiateCert)
<clientCertificateMappingAuthentication enabled="true" />

我在Windows RT应用程序中添加了客户端证书,如下所示:

//Install the self signed client cert to the user certificate store
string CACertificate = null;
try
{
    Uri uri = new Uri("ms-appx:///Assets/AdventureWorksTestClient1.pfx");
    var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(uri);
    IBuffer buffer = await FileIO.ReadBufferAsync(file);
    using (DataReader dataReader = DataReader.FromBuffer(buffer))
    {
       byte[] bytes = new byte[buffer.Length];
       dataReader.ReadBytes(bytes);
       // convert to Base64 for using with ImportPfx
       CACertificate = System.Convert.ToBase64String(bytes);
    }
    await CertificateEnrollmentManager.UserCertificateEnrollmentManager.ImportPfxDataAsync(
            CACertificate,
            "",
            ExportOption.Exportable,
            KeyProtectionLevel.NoConsent,
            InstallOptions.None,
            "ClientCert1");
 }
 catch (Exception ex)
 {...

然后我使用HttpBaseProtocolFilter,我以这种方式向其添加客户端证书:

IReadOnlyCollection<Certificate> certs = await CertificateStores.FindAllAsync(query);
HttpBaseProtocolFilter bpf = new HttpBaseProtocolFilter();
if (certs.Count > 0)
{
    cert = certs.ElementAt(0);
    bpf.ClientCertificate = cert;
}
HttpClient httpClient = new HttpClient(bpf);
....

然后请求:

var resp = await httpClient.GetAsync(new Uri(serviceURL));

这行代码正在生成此异常:

{System.Exception: Exception from HRESULT: 0x80072F7D
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
  at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
  at JumpStartCertificateDemo.MainPage.<btnCallService_Click>d__0.MoveNext()}

我100%确信我已经在本地主机(本地计算机)和应用程序端导入了正确的证书。通过浏览器调用服务工作正常。(系统会提示我提供客户端证书),所以在应用程序中提供客户端证书肯定有一些问题。

有人能帮我做这个吗?非常感谢。

使用Windows RT的客户端证书(Windows 8.1/Windows phone 8.1)

问题可能与您正在使用的证书的有效性有关。

默认情况下,.Net拒绝使用无效或不受信任的证书建立https连接。

通常,证书无效,因为它是由不受信任的颁发机构(自签名证书)生成的,或者因为站点地址未包含在证书的有效地址列表中。

在.Net中,可以放宽此限制,请参阅此讨论C#忽略证书错误?