使用HttpWebRequest设置.Net客户端身份验证

时间:2016-10-14 00:55:51

标签: c# authentication post client httpwebrequest

这更多是关于如何使HttpWebRequest工作,或者即使HttpWebRequest是正确的实现。我让C#和.Net技能在过去的几年里失效了,所以我希望我可以原谅我。

我试图点击需要客户端身份验证的安全网络服务。我有四个证书可以用。

•根证书 •中级根证书 •设备证书 •私钥

服务器是Java,这些证书是.jks形式的trustore和keystore。我将它们放入.pem文件中。

所以,我在C#客户端失败了,所以我想我会编写一些Python代码片段,以确保至少服务器端按预期工作。二十分钟后,我正在发帖。这是代码:

# Keys
path = "C:\\path\\"
key = path + "device.pem"
privkey = path + "device_privkey.pem"
CACerts = path + "truststore.concat" # root & intermediate cert


def post():
    url = "/url"
    headers = {'Content-Type': 'application/xml'}

    ## This section is HTTPSConnection

    context = ssl.SSLContext(ssl.PROTOCOL_TLS)
    context.verify_mode = ssl.CERT_OPTIONAL

    context.load_cert_chain(key, privkey, password='password')
    context.verify_mode = ssl.CERT_NONE
    context.load_verify_locations(CACerts)

    conn = http.client.HTTPSConnection(host, port=8080, context=context)
    conn.request("POST", url, registrationBody, headers)

    response = conn.getresponse()

    regresp = response.read()

concat证书是根证书和中间证书的串联。

你跟我在一起吗?

现在我的C#/。网络头痛。

这是我的尝试。我显然不知道我在这里做什么。

    public async Task POSTSecure(string pathname, string body)
    {
        string path = "C:\\path";
        string key = path + "device.pem";
        string privkey = path + "device_privkey.pem";
        string CACerts1 = path + "vtn_root.pem";
        string CACerts2 = path + "vtn_int.pem";

        try
        {
            // Create certs from files
            X509Certificate2 keyCert = new X509Certificate2(key);
            X509Certificate2 rootCert = new X509Certificate2(CACerts1);
            X509Certificate2 intCert = new X509Certificate2(CACerts2);

            HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://" + host + ":" + port + pathname);
            ServicePoint currentServicePoint = request.ServicePoint;

            // build the client chain?
            request.ClientCertificates.Add(keyCert);
            request.ClientCertificates.Add(rootCert);
            request.ClientCertificates.Add(intCert);

            Console.WriteLine("URI: {0}", currentServicePoint.Address);

            // This validates the server regardless of whether it should
            request.ServerCertificateValidationCallback = ValidateServerCertificate;

            request.Method = "POST";
            request.ContentType = "application/xml";
            request.ContentLength = body.Length;

            using (var sendStream = request.GetRequestStream())
            {
                sendStream.Write(Encoding.UTF8.GetBytes(body), 0, body.Length);
            }

            var response = (HttpWebResponse)request.GetResponse();
        }
        catch (Exception e)
        {
            Console.WriteLine("Post error.");
        }
    }

感谢您提供任何帮助或指向正确教程的指针。

[编辑]更多信息。在服务器端,调试指向空客户端证书链。这是在报告serverhello完成之后。

1 个答案:

答案 0 :(得分:1)

好吧,我觉得我原来很接近,但我这样解决了:

            request.ClientCertificates = new X509Certificate2Collection(
                                new X509Certificate2(
                                    truststore,
                                    password));

" trustore" file是包含上面列出的证书的.p12。可以通过keytool和openssl从.jks信任库创建.p12信任库。有很多关于如何做到这一点的信息。