简体   繁体   English

如何使用 RestSharp c# 验证 SSL 证书

[英]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.

相关问题 使用RestSharp对AC#客户端进行身份验证到Spring-boot服务器中 - Authenticate a c# client using RestSharp into Spring-boot server 如何使用Restsharp进行永久身份验证 - How to authenticate permanently using Restsharp 使用C#验证SSL证书 - Using C# to authenticate an SSL certificate 有没有一种方法可以使用FtpWebRequest使用C#中的客户端证书对FTP进行身份验证? - Is there a way to use FtpWebRequest to authenticate to FTP using client certificates in C#? Firefox中使用WebDriver和C#的不受信任的SSL证书 - Untrusted SSL Certificates in Firefox Using WebDriver and C# 如何使用IP地址而不是DNS(.net C#)获得SSL证书 - How to get ssl certificates using IP address instead of DNS (.net C#) 如何使用 C#、WMI 和/或 System.Management 从 IIS 6.0 获取站点列表和 SSL 证书? - How to get a list of sites and SSL certificates from IIS 6.0 using C#, WMI, and/or System.Management? 如何在C#中将SSL证书与HttpWebRequest一起使用? - How do I use SSL certificates with HttpWebRequest in C#? 如何在C#请求中包括两个SSL证书 - How to include two SSL certificates in C# request C#Metro应用程序中的Windows SSL证书 - Windows SSL Certificates in C# Metro Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM