首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web Service >

关于c#读取用户证件,并建立https连接

2012-09-15 
关于c#读取用户证书,并建立https连接对实现https连接很陌生,因为需要建立ssl通讯,不会!现在搞了份读证书的

关于c#读取用户证书,并建立https连接
对实现https连接很陌生,因为需要建立ssl通讯,不会!
现在搞了份读证书的代码,但很多代码也不是很明白,希望高手能写个注释!感激,代码如下!
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {
  Response.Write("OK");
  StreamReader sr = null;
  HttpWebResponse wr = null;

  HttpWebRequest hp = (HttpWebRequest)WebRequest.Create("https://mch.tenpay.com/cgi-bin/refund.cgi");
  ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
  Response.Write(hp.ClientCertificates.Count+"<br/>");
  hp.ClientCertificates.Add(new X509Certificate2("C:\\Documents and Settings\\Administrator\\桌面\\机票解决方案5166\\1202952101.pfx", "1202952101"));
  Response.Write(hp.ClientCertificates.Count + "<br/>");
   
  wr = (HttpWebResponse)hp.GetResponse();
  sr = new StreamReader(wr.GetResponseStream(), System.Text.Encoding.GetEncoding("GBK"));

   
  this.TextBox1.Text = sr.ReadToEnd();
  sr.Close();
  wr.Close();

  }

  public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
  {
  return true;
  }
 
}

public class MyPolicy : ICertificatePolicy
{
  public bool CheckValidationResult(
  ServicePoint srvPoint
  , X509Certificate certificate
  , WebRequest request
  , int certificateProblem)
  {  
  return true;
  } 
}

[解决办法]
关注下 帮顶了
[解决办法]

[解决办法]

探讨
c# https ssl这方面的技术,csdn上没高手吗.哎~

[解决办法]
hp.ClientCertificates.Add(new X509Certificate2("C:\\Documents and Settings\\Administrator\\桌面\\机票解决方案5166\\1202952101.pfx", "1202952101"));

这句就是读取客户端证书,至于通讯。NET已经有封装了。你可以查查MSDN有说明

热点排行