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

mar*_*ani 42 c# client-certificates windows-runtime windows-8.1 windows-phone-8.1

我正在尝试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" />
Run Code Online (Sandbox Code Playgroud)

我在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)
 {...
Run Code Online (Sandbox Code Playgroud)

然后我使用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);
....
Run Code Online (Sandbox Code Playgroud)

然后请求:

var resp = await httpClient.GetAsync(new Uri(serviceURL));
Run Code Online (Sandbox Code Playgroud)

这行代码生成此异常:

{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()}
Run Code Online (Sandbox Code Playgroud)

我100%确定我还在localhost(本地计算机)和应用程序端导入了正确的证书.通过浏览器调用服务正常.(我被提示提供客户端证书),因此在应用程序中提供客户端证书时必须存在一些问题.

有人可以帮我这个吗?谢谢.

cri*_*llo 1

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

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

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

在.Net中这个限制可以放宽,请参阅此讨论 C# Ignorecertificateserrors?