请求已中止:无法创建SSL / TLS安全通道。 (RestSharp,SSL客户端证书)

时间:2016-12-06 06:46:37

标签: authentication ssl certificate restsharp

我有以下代码,它使用基本身份验证和SSL客户端证书调用API,但它抛出异常并给我以下错误。

“请求已中止:无法创建SSL / TLS安全通道。”

我试图在Google上找到解决方案但未能找到任何解决方案。任何人都可以帮我解决这个问题。感谢。

// Variables 

string basicAuthenticationUserName = "username";
string basicAuthenticationPassword = "password";
string clientCertificateFilePath = "Path-To-Certificate-File";
string clientCertificatePassword = "certificate-password";
string url = "https://" + basicAuthenticationUserName + ":" + basicAuthenticationPassword + "@apiserverurl/apimethod";

// Creating RestSharp Request Object 

var request = new RestRequest(Method.POST)
{
    RequestFormat = DataFormat.Json,
    OnBeforeDeserialization = resp =>
    {
        resp.ContentType = "application/json";
    }
};

// Adding Headers

request.AddHeader("Content-Length", "0");
request.AddHeader("Accept", "application/x-null-message");

// Importing Certificates

var certificates = new X509Certificate();
certificates.Import(clientCertificateFilePath, clientCertificatePassword, X509KeyStorageFlags.PersistKeySet);

// Creating RestSharp Client Object

var client = new RestClient
{
    BaseUrl = new Uri(url),
    ClientCertificates = new X509CertificateCollection { certificates },
    Authenticator = new HttpBasicAuthenticator(managingLou, basicAuthenticationPassword)
};

// Executing Request 

var response = client.Execute<T>(request);

1 个答案:

答案 0 :(得分:0)

我遇到过类似的问题。让我在这里提一下您的帮助。

安装Windows服务后,我通过以下步骤解决了问题:

  • 转到开始&gt;运行并键入Services.msc
  • 选择您的服务&gt;右键单击并选择“属性”
  • 选择第二个选项卡“登录”
  • 选择单选按钮“此帐户”
  • 输入当前登录用户的用户名和密码。 (确保其与安装该服务的用户相同)
  • 应用更改
  • 启动服务