可编程地在Windows 8应用程序中使用SSL证书

时间:2014-01-10 10:00:54

标签: c# ssl windows-8 windows-runtime windows-8.1

我需要帮助:

我们的后端由自签名证书担保。让我们称之为:OurMegaCoolCertificate.cer

因此,我们已使用 certmgr.msc 将此证书导入我们的开发人员计算机。现在我们可以使用以下代码从后端检索数据:

async public static Task<string> getData(string Id, string Type)
    {
        String url = "https://BACKEND/API/?Id=" + Id + "&Type=" + Type;
        HttpClientHandler aHandler = new HttpClientHandler();
        aHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
        HttpClient aClient = new HttpClient(aHandler);
        aClient.DefaultRequestHeaders.ExpectContinue = false;

        aClient.DefaultRequestHeaders.MaxForwards = 3;
        Uri requestUri = new Uri(url);
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUri);
        //request.Headers.ExpectContinue = false;
        var result = await aClient.GetAsync(requestUri, HttpCompletionOption.ResponseContentRead);

        var responseHeader = result.Headers;
        //Debug.WriteLine(responseHeader.WwwAuthenticate);

        var responseBody = await result.Content.ReadAsStringAsync();

        return responseBody;
    }

但是我们无法告诉用户我们的应用程序,手动安装证书,有没有办法将此证书添加到项目并使用它?或者导入到用户机程序?请指导我,我是SSL安全新手

我已设法做到这一点,没有错误,但请求失败,看起来请求找不到证书:

private async void GetOverHere()
    {
        //await Windows.Security.Cryptography.Certificates.CertificateEnrollmentManager.InstallCertificateAsync("",InstallOptions.None);
        StorageFolder packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
        StorageFolder certificateFolder = await packageLocation.GetFolderAsync("Certificates");
        StorageFile certificate = await certificateFolder.GetFileAsync("OurMegaCoolCertificate.cer");

        IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(certificate);
        string encodedString = Windows.Security.Cryptography.CryptographicBuffer.EncodeToBase64String(buffer);

        await Windows.Security.Cryptography.Certificates.CertificateEnrollmentManager.InstallCertificateAsync(encodedString, InstallOptions.None);


    }

我们也尝试在清单中执行此操作:

</Capabilities>
<Extensions>
<!--Certificates Extension-->
 <Extension Category="windows.certificates">
  <Certificates>
    <Certificate StoreName="Root" Content="Assets\OurMegaCoolCertificate.cer" />

  </Certificates>
</Extension>

再次,当我们使用certmgr.msc导入受信任的根证书时 - 所有工作

1 个答案:

答案 0 :(得分:5)

我设法让这个工作:

我在packagemanifest中添加了这个:

</Capabilities>
<Extensions>
<!--Certificates Extension-->
<Extension Category="windows.certificates">
   <Certificates>
       <Certificate StoreName="Root" Content="Assets\OurMegaCoolCertificate.cer" />
   </Certificates>
</Extension>

但是我已经导出了我的证书而不是使用DER(或类似的东西),但作为base64并且它有效。但是所有教程都说需要导出为DER ...