zoukankan      html  css  js  c++  java
  • C#中实现https的双向认证

    1.  把浏览器中的证书导出为cer文件。

    2.   代码如下:

    1. using System;  
    2. using System.Net;  
    3. using System.IO;  
    4. using System.Security.Cryptography.X509Certificates;  
    5. using System.Text;  
    6. using System.Net.Security;  
    7.   
    8. public partial class About : System.Web.UI.Page  
    9. {  
    10.     protected void Page_Load(object sender, EventArgs e)  
    11.     {  
    12.        // string url = "https://192.168.6.120/";  
    13.         ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallback;  
    14.         //Uri uri = new Uri("https://www.baifubao.com/");  
    15.   
    16.   
    17.         Uri uri = new Uri("https://192.168.6.120/");  
    18.         HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);  
    19.         X509Certificate cer = new X509Certificate("F:\csharp2.cer");  
    20.         request.ClientCertificates.Add(cer);  
    21.   
    22.         HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
    23.         string encoding = response.ContentEncoding;  
    24.         if (encoding == null || encoding.Length < 1)  
    25.         {  
    26.             encoding = "UTF-8"; //默认编码  
    27.         }  
    28.         StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));  
    29.         Response.Write(reader.ReadToEnd());  
    30.         //Console.Write(reader.ReadToEnd());  
    31.         response.Close();   
    32.     }  
    33.     public static bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)  
    34.     {  
    35.         if (sslPolicyErrors == SslPolicyErrors.None)  
    36.             return true;  
    37.         return false;  
    38.     }  
  • 相关阅读:
    有关于CSS的面试题和练习
    Yslow&PageSpeed– 诊断各种缓慢症状
    使用Plant Simulation连接SQL Server
    利用Microsoft Sql Server Management studio 创建数据库的示例
    SQL2008配置管理工具服务显示远程过程调用失败
    用C语言的rand()和srand()产生伪随机数的方法总结
    Fisher–Yates shuffle 洗牌算法(zz)
    Unity3D导入MAX文件的一些问题(zz)
    UG中STP203和STP214的区别
    生产线工序基础知识
  • 原文地址:https://www.cnblogs.com/kunlunmountain/p/6437068.html
Copyright © 2011-2022 走看看