c#获取ssl证书有效性_问题SSL证书C#

这篇博客介绍了如何在C#应用程序中通过HTTPS调用Web服务时使用.crt文件验证服务器证书的有效性。作者提供了设置SSL证书和自定义验证回调的方法,并给出了发送POST请求的示例代码。特别感谢Dipti Mehta的帮助,该解决方案解决了作者的问题。
摘要由CSDN通过智能技术生成

在我的C#应用​​程序中,我必须通过https调用Web服务并使用我已经拥有的.crt文件进行验证.以下是满足此类需求的正确解决方案.我得到一个有效的解决方案后,我已经更新了这篇文章,认为它可能会帮助像我这样的人.

解决方案

以下代码必须在整个应用程序执行中只执行一次.有了这个,我们设置ServerCertification和SSL属性,每当调用reqest时将使用它们:

public static void setSSLCertificate()

{

clientCert = new X509Certificate2(AUTHEN_CERT_FILE); // Pointing to the .crt file that will be used for server certificate verification by the client

System.Net.ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);

}

public static bool customXertificateValidation(Object sender, X509Certificate certificate, X509Chain chain, System.Net.Security.SslPolicyErrors sslPoicyErrors)

{

switch (sslPoicyErrors)

{

case System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors:

case System.Net.Security.SslPolicyErrors.RemoteCertificateNameMismatch:

case System.Net.Security.SslPolicyErrors.RemoteCertificateNotAvailable:

break;

}

return clientCert.Verify(); // Perform the Verification and sends the result

}

请求通常像我们没有实现SSL那样完成.这是一个Post请求代码:

private static String SendPost(String uri, String post_data)

{

String resData = "";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

request.KeepAlive = false;

request.ProtocolVersion = HttpVersion.Version10;

request.ContentType = "application/x-www-form-urlencoded";

request.Method = "POST";

// turn request string into byte[]

byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

Stream requestStream = null;

try

{

// Send it

request.ContentLength = postBytes.Length;

requestStream = request.GetRequestStream();

requestStream.Write(postBytes, 0, postBytes.Length);

}

catch (WebException we)

{ // If SSL throws exception that will be handled here

if (we.Status == WebExceptionStatus.TrustFailure)

throw new Exception("Exception Sending Data POST : Fail to verify server " + we.Message);

}

catch (Exception e)

{

throw new Exception("Exception Sending Data POST : " + e.Message, e.InnerException);

}

finally

{

if (requestStream != null)

requestStream.Close();

}

// Get the response

HttpWebResponse response = null;

try

{

response = (HttpWebResponse)request.GetResponse();

if (response == null)

return "";

StreamReader sr = new StreamReader(response.GetResponseStream());

resData = sr.ReadToEnd().Trim();

sr.Close();

}

catch (Exception e)

{

throw new Exception("Error receiving response from POST : " + e.Message, e.InnerException);

}

finally

{

if (response != null)

response.Close();

}

return resData;

}

特别感谢Dipti Mehta,他的探索帮助我通过接受服务器证书来实现目标.她帮助我解决了我的困惑.我终于找到了如何使用客户端使用.crt文件验证服务器证书.

希望这有助于某人.

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值