【问题标题】:Mutual TLS connection code - Convert working python code to C#相互 TLS 连接代码 - 将工作 python 代码转换为 C#
【发布时间】:2018-03-14 01:47:31
【问题描述】:

下面的客户端代码在 python 中工作,并使用双向 TLS 连接连接到远程服务器,其中客户端拥有私钥和公钥,远程服务器拥有公钥。

有谁知道如何用 C# 编写这段代码 使用 keyfile\cer 组合或 .pfx 文件。

import ssl
import urllib
from http import client
import requests

key_file_path = "*[Path]*\Keyfile.key"
cert_file_path = "*[Path]*\ird_taxlab_co_nz.crt"

response = requests.get('*[URL OF SERVICE]*', cert=(cert_file_path, key_file_path))

【问题讨论】:

标签: c# python tls1.2


【解决方案1】:

如果您想将该示例转换为 c#,您应该能够对 HttpWebRequestX509Certificate2Collection 执行相同操作。

首先导入证书:

string certPath = <pfx path>;
string certPass = <password>;
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);

之后创建HttpWebRequest 并为其添加证书:

WebRequest request = WebRequest.Create(/*your URI*/);
request.AuthenticationLevel = AuthenticationLevel.MutualAuthRequired;    
HttpWebRequest httpWebRequest = (HttpWebRequest)request;
httpWebRequest.ClientCertificates.Add(/*take cert from collection*/);    
HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();

在此之后,您将获得可以处理和收集结果的响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-05
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多