簡體   English   中英

使用Windows RT的客戶端證書(Windows 8.1 / Windows Phone 8.1)

[英]Using Client certificates for Windows RT (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" />

我在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%確定我還在localhost(本地計算機)和應用程序端導入了正確的證書。 通過瀏覽器調用服務正常。 (我被提示提供客戶端證書),因此在應用程序中提供客戶端證書時必須存在一些問題。

有人可以幫我這個嗎? 謝謝。

問題可能與您使用它的證書的有效性有關。

默認情況下.Net拒絕與無效或不可信證書建立https連接。

通常證書無效,因為它是由不受信任的機構(自簽名證書)生成的,或者因為該站點的地址未包含在證書的有效地址列表中。

在.Net中可以放寬此限制,請參閱此討論C#忽略證書錯誤?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM