下面的客户端代码在python中工作,并使用相互TLS连接连接到远程服务器,其中客户端具有私钥和公钥,远程服务器具有公钥。
有谁知道如何用C#编写这段代码 使用keyfile \ cer combo或.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))
答案 0 :(得分:0)
如果您想将该示例转换为c#,您应该可以对HttpWebRequest
和X509Certificate2Collection
执行相同操作。
首先导入证书:
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();
在此之后,您可以处理并收集结果的响应。