UWP Application for HttpClient HTTPS Applications

I am writing a UWP application in C # that is ultimately designed for IoT, but for now I am only debugging locally. I use Windows.Web.Http.HttpClientWCF REST to connect to the self-service web service WCF, which I also wrote and run as a console application on the same machine for testing. The service requires mutual authentication with certificates, so I have a CA certificate, a service certificate, and a client certificate.

My UWP code works as follows:

  • Check for a certificate certificate for client certificates and CA certificates.
  • If not, install from the PFX file and the CER file, respectively.
  • Attach Certificateto HttpBaseProtocolFilterand add a filter toHttpClient
  • Call HttpClient.PostAsync

After the call PostAsyncthe following error: An Error Occurred in the Secure Channel Support. After much searching on the Internet and with common sense, I am sure that HttpClientbarfing is due to a problem with establishing a mutually authenticated SSL connection. But based on my troubleshooting, I can't figure out why.

To continue, I wrote a regular old console application using System.Net.Http.HttpClient, attached the client certificate to the request and everything works fine. Unfortunately, it is System.Netnot fully supported in UWP. I also tried NOT attaching the certificate to UWP HttpClient, and the application asks me with a user interface to select the installed certificate. I select the correct certificate and still get the same exception (this, at least, lets me know that the certificate is installed correctly and is validated correctly by the CA from the point of view of the application). In addition, I hit GET on a web service from a browser, select a client certificate when prompted, and can upload the file.

Fiddler, , , - , , , , - ( , Fiddler ). Wireshark, , Wireshark localhost Windows.

- -, , .

: Windows.Web.Http.HttClient ? , , - HTTP, ?

+4
2

MSDN . , MS API . , .

http://blogs.msdn.com/b/wsdevsol/archive/2015/03/26/how-to-use-a-shared-user-certificate-for-https-authentication-in-an-enterprise-application.aspx

:

, , . , , , .

Windows Runtime, , - , ?

, "" . CryptographicEngine.SignAsync, , , , . , Async , : Sign, , .

:

public static async Task<bool> VerifyCertificateKeyAccess(Certificate selectedCertificate)
{
    bool VerifyResult = false;  // default to access failure
    CryptographicKey keyPair = await PersistedKeyProvider.OpenKeyPairFromCertificateAsync(
                                        selectedCertificate, HashAlgorithmNames.Sha1, 
                                        CryptographicPadding.RsaPkcs1V15);
    String buffer = "Data to sign";
    IBuffer Data = CryptographicBuffer.ConvertStringToBinary(buffer, BinaryStringEncoding.Utf16BE);

    try
    {
        //sign the data by using the key
        IBuffer Signed = await CryptographicEngine.SignAsync(keyPair, Data);
        VerifyResult = CryptographicEngine.VerifySignature(keyPair, Data, Signed);
    }
    catch (Exception exp)
    {
        System.Diagnostics.Debug.WriteLine("Verification Failed. Exception Occurred : {0}", exp.Message);
        // default result is false so drop through to exit.
    }

    return VerifyResult;
}

, , .

+3
  • Manifested ( )
  • Frist Call of in Ur Project . .

          {

            var filter = new HttpBaseProtocolFilter();
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Expired);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.InvalidName);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationFailure);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.RevocationInformationMissing);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.WrongUsage);
            filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.IncompleteChain);

            Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);
            TimeSpan span = new TimeSpan(0, 0, 60);
            var cts = new CancellationTokenSource();
            cts.CancelAfter(span);
            var request = new Windows.Web.Http.HttpRequestMessage()
            {
                RequestUri = new Uri(App.URL + "/oauth/token"),
                Method = Windows.Web.Http.HttpMethod.Post,
            };
            //request.Properties. = span;
            string encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(Server_Username + ":" + Server_Password));
            var values = new Dictionary<string, string>
                { { "grant_type", "password" },{ "username",  Uname}, { "password", Pwd }};
            var content = new HttpFormUrlEncodedContent(values);
            request.Headers.Add("Authorization", "Basic " + encoded);
            request.Content = content;
            User root = new User();
            using (Windows.Web.Http.HttpResponseMessage response = await client.SendRequestAsync(request).AsTask(cts.Token))
            {
                HttpStatusCode = (int)response.StatusCode;
                if (HttpStatusCode == (int)HttpCode.OK)
                {
                    using (IHttpContent content1 = response.Content)
                    {
                        var jsonString = await content1.ReadAsStringAsync();
                        root = JsonConvert.DeserializeObject<User>(jsonString);
                        App.localSettings.Values["access_token"] = root.Access_token;
                        App.localSettings.Values["refresh_token"] = root.Refresh_token;
                        App.localSettings.Values["expires_in"] = root.Expires_in;
                        var json = JsonConvert.SerializeObject(root.Locations);
                        App.localSettings.Values["LocationList"] = json;
                        App.localSettings.Values["LoginUser"] = Uname;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
0

All Articles