[英]How to authenticate SSL certificates using RestSharp c#
The location of the certificates证书的位置
Inherit auth from parent从父级继承身份验证
The test in postman postman中的测试
I want to use Restsharp to send this GET request -我想使用 Restsharp 发送这个 GET 请求 -
This is what I want to send -这就是我要发送的 -
Request - GET,请求- 获取,
URL - https://18.0.1.230:8080/api/report/test , URL - https://18.0.1.230:8080/api/report/test ,
Header - Key = Content-Type, Value = application/x-www-form-urlencoded Header - 键 = 内容类型,值 = 应用程序/x-www-form-urlencoded
On Postman, I am not sending anything on Authorisation type (its set on "Inherit auth from parent") check 'inherit auth from parent' image在 Postman 上,我没有发送关于授权类型的任何内容(其设置为“从父级继承身份验证”)检查“从父级继承身份验证”图像
I have set Client certificates in Postman, so I have set the location of them, which postman uses when sending this request.我在 Postman 中设置了客户端证书,所以我设置了它们的位置,postman 在发送此请求时使用它。 Check 'Location of certificate' image
检查“证书位置”图像
I want to know how do I add these certificates to a GET request using restSharp c# code, so that I pass the authentication and can get a 200 response?我想知道如何使用 restSharp c# 代码将这些证书添加到 GET 请求中,以便通过身份验证并获得 200 响应?
What you need to do is first import the certificates:您需要做的是首先导入证书:
public static async Task<X509Certificate2> LoadPemCertificate(string certificatePath, string privateKeyPath)
{
using var publicKey = new X509Certificate2(certificatePath);
var privateKeyText = await File.ReadAllTextAsync(privateKeyPath);
var privateKeyBlocks = privateKeyText.Split("-", StringSplitOptions.RemoveEmptyEntries);
var privateKeyBytes = Convert.FromBase64String(privateKeyBlocks[1]);
using var rsa = RSA.Create();
if (privateKeyBlocks[0] == "BEGIN PRIVATE KEY")
{
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);
}
else if (privateKeyBlocks[0] == "BEGIN RSA PRIVATE KEY")
{
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
}
var keyPair = publicKey.CopyWithPrivateKey(rsa);
return new X509Certificate2(keyPair.Export(X509ContentType.Pfx));
}
Once you're you have loaded the certificate in memory, all you have to do is attach certificate to restsharp client:一旦你在 memory 中加载了证书,你所要做的就是将证书附加到 restsharp 客户端:
var client = new RestClient("https://18.0.1.230:8080/api/report/test");
client.ClientCertificates.Add(new X509Certificate(certificate));
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.