Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HttpClient请求中未包含嵌入式证书(c)_C#_.net_Ssl - Fatal编程技术网

C# HttpClient请求中未包含嵌入式证书(c)

C# HttpClient请求中未包含嵌入式证书(c),c#,.net,ssl,C#,.net,Ssl,我正在尝试向外部API发送SSL证书,以便通过其服务进行身份验证。我收到错误:“请求被中止:无法创建SSL/TLS安全通道。” 我与API的托管公司进行了交谈,他们说他们更改了服务以接受任何SSL证书,只是为了确保我确实发送了SSL证书,我收到了相同的错误,因此请求中似乎没有包含SSL证书 var clientHandler = new WebRequestHandler(); clientHandler.ClientCertificates.Add(GetEmbeddedCert()); S

我正在尝试向外部API发送SSL证书,以便通过其服务进行身份验证。我收到错误:“请求被中止:无法创建SSL/TLS安全通道。”

我与API的托管公司进行了交谈,他们说他们更改了服务以接受任何SSL证书,只是为了确保我确实发送了SSL证书,我收到了相同的错误,因此请求中似乎没有包含SSL证书

var clientHandler = new WebRequestHandler();
clientHandler.ClientCertificates.Add(GetEmbeddedCert());

ServicePointManager.Expect100Continue = true;
System.Net.ServicePointManager.SecurityProtocol = 
SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
ServicePointManager.ServerCertificateValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => true;

using (var client = new HttpClient(clientHandler))
{
     return client.PostAsync(_plugin.EndPoint.ToString().Trim(),
     new StringContent(hl7)).Result;                
}
我使用的嵌入式证书的构建操作设置为“嵌入式资源”:

private X509Certificate2 GetEmbeddedCert()
{
使用(Stream=Assembly.getExecutionGassembly()
.GetManifestResourceStream(GetType(),“mycert.cer”))
{
字节[]字节=新字节[stream.Length];
for(intidx=0;idx
有人知道为什么SSL证书没有被发送,或者为什么我会出现上述错误吗


谢谢

您可以尝试在web浏览器中使用证书吗?它有效吗?由于某种原因,当我在浏览器中导航到url时,证书不在弹出的证书列表中,但我使用了另一个证书,我得到了一个405方法,不允许@markokatar
private X509Certificate2 GetEmbeddedCert()
    {
        using (Stream stream = Assembly.GetExecutingAssembly()
       .GetManifestResourceStream(GetType(), "mycert.cer"))
        {
            byte[] bytes = new byte[stream.Length];
            for (int idx = 0; idx < stream.Length; idx++)
                bytes[idx] = (byte)stream.ReadByte();

            X509Certificate2 cert = new X509Certificate2();
            cert.Import(bytes);
            return cert;
        }
    }