简体   繁体   English

在.Net Compact Framework中使用X509证书进行客户端身份验证HTTPRequest

[英]Using a X509 Certificate in .Net Compact Framework for Client Authentication HTTPRequest

I'm working in Windows Mobile 6 and would like to have client authentication when talking to a Apache webserver. 我正在使用Windows Mobile 6,并且想与Apache Web服务器通信时进行客户端身份验证。 I have a certificate in my local certificate store and it should be rather straightforward: 我在本地证书存储区中有一个证书,应该很简单:

X509Store myStore = new X509Store("MY", StoreLocation.CurrentUser);
myStore.Open(OpenFlags.ReadOnly);
X509Certificate2Collection certificates = myStore.Certificates;
X509Certificate2 clientcertificate;
foreach (X509Certificate 2certificate in certificates) {
     clientcertificate = certificate; //omitted code to validate certificate
}
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(webPage);
req.AllowWriteStreamBuffering = true;
req.AllowAutoRedirect = false;
req.Method = "POST";
req.ContentType = "text/xml";
req.Accept = "text/xml";
req.ClientCertificates.Add(clientcertificate);
Stream stream = req.GetRequestStream();
stream.Write(buffer, 0, buffer.Length);
stream.Close();

This code segment works as long as I remove the "req.ClientCertificates.Add(clientcertificate)" line. 只要我删除“ req.ClientCertificates.Add(clientcertificate)”行,此代码段就可以工作。

Once that's inserted, I get a "Could not establish secure channel for SSL/TLS". 插入后,我得到“无法为SSL / TLS建立安全通道”。 Maddeningly enough, when I use this exact code in the regular .Net Framework it transmits the certificate perfectly. 令人发疯的是,当我在常规.Net Framework中使用此确切代码时,它可以完美地传输证书。

Does anyone know if this is possible in Compact Framework? 有谁知道在Compact Framework中是否可行? If I can't present the X509Certificate for Client Authentication, what other ways should I pursue to ensure that authentication is proper (I should have access to CAPI or other Microsoft Cryptographic modules) 如果我不能出示用于客户端身份验证的X509证书,则应采取什么其他方式来确保身份验证正确(我应该可以访问CAPI或其他Microsoft加密模块)

Thanks. 谢谢。

It could be that your Apache Server does not support secure connections. 可能是您的Apache服务器不支持安全连接。

For example, I've got a few websites on hosted domains that cost little or nothing. 例如,我在托管域名上有一些网站,价格不高或根本没有。 I use these websites to test code on all the time. 我一直在使用这些网站来测试代码。

But, to get SSL capabilities, I've got to shell out like $50/month. 但是,要获得SSL功能,我必须支付每月50美元的费用。 So, I can't test those on my sites. 因此,我无法在自己的网站上对其进行测试。

To test: If the Apache Server supports SSL, you should be able to replace the URL with the SSL equivalent: http://www.stackoverflow.com with https://www.stackoverflow.com 测试:如果Apache服务器支持SSL,则您应该能够使用等效的SSL替换URL:用https://www.stackoverflow.com替换http://www.stackoverflow.com

Good news: I solved it. 好消息:我解决了。 It turned out not to have to do with the .Net Compact Framework. 事实证明,这与.Net Compact Framework无关。 In 3.5 CF, HTTPWebRequest.ClientCertificates is supported as long as the X509 certificate can be accessed. 在3.5 CF中,只要可以访问X509证书,就支持HTTPWebRequest.ClientCertificates。

The reason why the SSL Handshake failed was because of a trust issue with the server side certificate. SSL握手失败的原因是服务器端证书的信任问题。 Our server certificates were self-signed and we used certificates that were signed for the wrong URL, so the application rightly wouldn't trust the provided server certificate. 我们的服务器证书是自签名的,我们使用为错误的URL签名的证书,因此应用程序正确地不会信任提供的服务器证书。 For testing purposes, we put in place a Trust All Certificates policy, which will be removed for the production. 为了进行测试,我们制定了“信任所有证书”策略,该策略将在生产中删除。

sealed class AcceptAllCertificatePolicy : ICertificatePolicy
{
    private const uint CERT_E_UNTRUSTEDROOT = 0x800B0109;

    public bool CheckValidationResult(ServicePoint srvPoint, X509Certificate
    certificate, WebRequest request, int certificateProblem)
    {
        // Just accept.
        return true;
    }
    /*public bool CheckValidationResult(ServicePoint sp,
    X509Certificate cert, WebRequest req, int problem)
    {
        return true;  
    }*/
}

Referenced right before the HTTPWebRequest 在HTTPWebRequest之前引用

System.Net.ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();

And this solves our problem with the SSL/TLS secure channel. 这就解决了SSL / TLS安全通道的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .net客户端使用X509证书和用户名/密码身份验证调用JAX-WS Web服务 - .net client invoke JAX-WS webservice with X509 certificate and username/password authentication 当发行者不是X509时,使用证书存储使用Microsoft .NET框架进行客户端身份验证的受信任根 - Use certificate when issuer is not is X509Store trusted roots for client authentication using Microsoft .NET framework 无法使用X509客户端证书连接到HTTPS - Can't connect to HTTPS using X509 client certificate X509Certificate和.NET Compact Framework 3.5 - X509Certificate and .NET Compact Framework 3.5 使用X509证书进行解密 - Using a X509 certificate for decryption 使用Rsa和x509证书的ProtectedConfigurationProvider - ProtectedConfigurationProvider using Rsa and x509 certificate 使用X509证书的邮件签名 - Message Signing using X509 certificate 如何在Windows的.net框架中使用ECC X509证书中的公钥加密数据? - How can I encrypt data using a public key from ECC X509 certificate in .net framework on windows? 使用X509Certificate2在https中进行客户端证书认证 - client certificate authentication in https using X509Certificate2 在Webrole上的Azure WCF服务上基于x509证书的身份验证 - x509 certificate based authentication on Azure WCF service on webrole
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM