OpenSSL证书缺少PrivateKey C#

OpenSSL证书缺少PrivateKey C#,c#,.net-core,openssl,C#,.net Core,Openssl,我有一个密码: string certificateText = File.ReadAllText("./Certificates/cert.pem"); string privateKeyText = File.ReadAllText("./Certificates/key.pem"); var provider = new CertificateFromFileProvider(certificateTe

我有一个密码:

        string certificateText = File.ReadAllText("./Certificates/cert.pem");
        string privateKeyText = File.ReadAllText("./Certificates/key.pem");
        var provider = new CertificateFromFileProvider(certificateText, privateKeyText);
        certificate = provider.Certificate;
当我检查私钥字段时,得到空值。privateKeyText也有价值。

通过生成p12证书解决

openssl pkcs12 -export -in cert.pem -inkey "privateKey.pem" -out myProject_keyAndCertBundle.p12
然后更改了我的代码

    public static string userId = "6L29##############################SfkI";
    public static string password = "87t##########GLV";

    public static string cert = "./Certificates/myProject_keyAndCertBundle.p12";
    public static string certificatePassword= "####Password#######";


 HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
        request.ContentType = "application/json";
        request.Accept = "application/json";

        request.Method = method;
        if (method.Equals("POST") || method.Equals("PUT"))
        {
            // Load the body for the post request
            var requestStringBytes = Encoding.UTF8.GetBytes(requestBodyString);
            request.GetRequestStream().Write(requestStringBytes, 0, requestStringBytes.Length);
        }

        if (headers != null)
        {
            foreach (KeyValuePair<string, string> header in headers)
            {
                request.Headers[header.Key] = header.Value;
            }
        }

        // Add headers
        request.Headers["Authorization"] = GetBasicAuthHeader(userId, password);

        // Add certificate
        var certificate = new X509Certificate2(certificatePath, certificatePassword);
        request.ClientCertificates.Add(certificate);

        try
        {
            // Make the call
            using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
            {
                LogResponse(testInfo, response);
                statusCode = response.StatusCode.ToString();
            }
        }
        catch (WebException e)
        {
            if (e.Response is HttpWebResponse)
            {
                HttpWebResponse response = (HttpWebResponse)e.Response;
                LogResponse(testInfo, response);
                statusCode = response.StatusCode.ToString();
            }
        }
        return statusCode;
    }
公共静态字符串userId=“6L29”; 公共静态字符串密码=“87t#########GLV”; 公共静态字符串cert=“./Certificates/myProject\u keyAndCertBundle.p12”; 公共静态字符串证书密码=“密码”; HttpWebRequest-request=WebRequest.Create(requestURL)作为HttpWebRequest; request.ContentType=“application/json”; request.Accept=“application/json”; request.Method=Method; if(method.Equals(“POST”)| method.Equals(“PUT”)) { //加载post请求的正文 var requestStringBytes=Encoding.UTF8.GetBytes(requestBodyString); request.GetRequestStream().Write(requestStringBytes,0,requestStringBytes.Length); } 如果(标题!=null) { foreach(标头中的KeyValuePair标头) { request.Headers[header.Key]=header.Value; } } //添加标题 request.Headers[“Authorization”]=GetBasicAuthHeader(用户ID、密码); //添加证书 var证书=新的X509Certificate2(certificatePath,certificatePassword); request.ClientCertificates.Add(证书); 尝试 { //打电话 使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse) { LogResponse(testInfo,response); statusCode=response.statusCode.ToString(); } } 捕获(WebE例外) { 如果(例如,响应为HttpWebResponse) { HttpWebResponse=(HttpWebResponse)e.response; LogResponse(testInfo,response); statusCode=response.statusCode.ToString(); } } 返回状态码; }
您看到的是两个不同的对象。VS中的私钥以大写字母“P”开头“虽然代码的小写字母为‘p’。@jdweng实际上我在API上遇到了400个错误,我想这是因为证书上缺少PrivateKey。”。我没有在代码上使用公钥,RestClient400需要公钥。这是一个错误的请求,可能是因为TLS身份验证失败。如果证书不正确,TLS将失败。@jdweng已经在postman上测试过,证书是正确的。如果postman正在工作,则使用诸如wireshark或fiddler之类的嗅探器,在c#和postman之间比较第一个请求中的TLS版本和头。让c#看起来像邮递员。标题和TLS默认值在c#和Postman中不同。