c# - 为 Windows RT (windows 8.1/windows phone 8.1) 使用客户端证书

标签 c# windows-runtime windows-8.1 windows-phone-8.1 client-certificates

我正在尝试 Windows 8.1 和 Windows Phone 8.1 的一项新功能,即证书存储和在服务器端使用客户端证书进行客户端身份验证的可能性。但是我在使用此功能时遇到问题。

我有一个在 IIS Express 上运行的经过基本测试的 WCF 服务。 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% 确定我已经在本地主机(本地计算机)和应用程序端导入了正确的证书。通过浏览器调用服务工作正常。 (系统提示我提供客户端证书),因此在应用程序中提供客户端证书一定存在问题。

谁能帮我解决这个问题?谢谢。

最佳答案

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

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

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

在 .Net 中可以放宽此限制,请参阅此讨论 C# Ignore certificate errors?

关于c# - 为 Windows RT (windows 8.1/windows phone 8.1) 使用客户端证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25764157/

相关文章:

javascript - 如何将任何格式的字节数组内容数据保存到winjs中的文件中并保存到Windows平板电脑中的本地文件夹

php - 在 XAMPP (WINDOWS 8.1) 中将 PHP 中的 Python 作为脚本运行

c# - 为什么是 "typeof(ISubController).IsAssignableFrom(value.GetType())"而不是 "value is ISubController"

c# - VS2015 C# string.Format 在使用转义大括号时抛出错误

c# - 什么是 Template10 导航服务的 NavigationServices.FirstOrDefault()?

c# - 在 Windows 8.1 上禁用触摸视觉反馈(以编程方式)[桌面应用程序]

c# - .NET 进程到 Borland C++ 进程通信

controls - Windows 8 Metro风格横向设计

c# - 如何更改 xaml 中定义的资源的值?

java - Windows 8.1 上的“卡住”套接字