全國身份證查詢系統nciis

把官方提共的地方输入到地址栏里,然后回车,然后会出现这个对话框,我们只要单击查看证书,就行了,在后面出现的界面 里单击安装就可以了

如果要导出则选择“复制到文件” ==下一步  “Base64编码x.509 (.cer)(s)”选择这一项下一步完成就OK了。

     然后把你的证书文件复制到一个位置方便使用。文件名假设为tingting.cer(cer表示证书文件);

    像这样的程序一般只提共Java版的代码,那我们c#的怎么办呢,哎,不过经过今天下午的努力总算是出了,现在给大家分享一下经验;

     看Java的代码不太明白 ,不过也能知道一些,现在是Wsdl文件要怎么得到呢?我是这样写的

   

代码
//请求的地址
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.cnblogs.com/sufei");

            //创建证书文件
            X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\tingting.cer");

            //添加到请求里
            request.ClientCertificates.Add(objx509);

            //User-AgentHTTP标头的值
            request.UserAgent = "Client Cert Sample";
            request.Method = "POST";

            //读返回的流
            StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream());

            //把得到的WSDL文件放到一个richTextBox1
            this.richTextBox1.Text = reader.ReadToEnd();
 

 Application.StartupPath + "tingting.cer/"  这里就是我们刚才导出的证书文件的地址,当然我们在实际应用中要写成自己的才行;

request.UserAgent = "Client Cert Sample"; User-AgentHTTP标头的值这个可以参考这里  http://support.microsoft.com/kb/895971

 

我直接把全部的代码拿下来吧,方便以后查询

 

代码
当 Web 服务器需要一个使用 HttpWebRequest 和 HttpWebResponse 类时,您可以发送客户端证书。 若要获取用于通过使用 HttpWebRequest 类发送客户端证书的证书,使用下列方法之一:
方法 1
使用 x509 证书 类来读取从.cer 文件的证书,然后将 ClientCertificates 属性设置。
方法 2
使用 CryptoAPI 调用来从证书存储获得证书,然后将 x509 证书 类设置为接收从证书存储区的证书。 然后,您可以设置 ClientCertificates 属性。
回到顶端
发送客户端证书的要求
可与 ASP.NET 应用程序时确保完成以下要求:
LOCAL_MACHINE 注册表配置单元中并不在 CURRENT_USER 注册表配置单元中,必须安装客户端证书。 若要确认客户端证书的安装位置,请按照下列步骤操作:
单击 开始、 单击 运行,键入 mmc,然后单击 确定。
在 文件 菜单上单击 添加/删除管理单元。
在 添加/删除管理单元 对话框中单击 添加。
在 添加独立管理单元 对话框中单击 证书,然后单击 添加。
在 证书管理单元 对话框中单击 计算机帐户,然后单击 下一步
在 选择计算机 对话框中单击 完成。
在 添加独立管理单元 对话框中单击 关闭,然后单击 确定。
展开 证书 (本地计算机),展开 个人,然后单击 证书。
在右窗格中应列出客户端证书。
您必须授予 ASP.NET 用户客户端证书的私钥的帐户权限。 若要为 ASP.NET 用户授予客户端证书的私钥的帐户权限,使用 WinHttpCertCfg.exe 工具。有关详细信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
823193  (http://support.microsoft.com/kb/823193/ ) 如何获得 Windows HTTP 5.1 证书和跟踪工具
有关如何使用此工具,请访问下面的 Microsoft 开发人员网络 (MSDN) 的网站的详细信息:
WinHttpCertCfg.exe,证书配置工具 http://msdn2.microsoft.com/en-us/library/aa384088.aspx (http://msdn2.microsoft.com/en-us/library/aa384088.aspx)
回到顶端
使用.cer 文件
方法 1 是易于使用,但方法要求您具有一个.cer 文件。 如果您没有安装的.cer 文件,使用 Microsoft Internet 资源管理器导出.cer 文件。

下列源代码介绍如何获取证书从一个.cer 文件您可以使用 HttpWebRequest class.
//Uncomment the following code if you need a proxy. The boolean true is used to bypass the local address.
//WebProxy proxyObject = new WebProxy("Your Proxy value",true);
//GlobalProxySelection.Select = proxyObject;

// Obtain the certificate.
try
{
    //You must change the path to point to your .cer file location.
    X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\mycert.cer");
    // Handle any certificate errors on the certificate from the server.
    ServicePointManager.CertificatePolicy = new CertPolicy();
    // You must change the URL to point to your Web server.
    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create("https://YourServer/sample.asp");
    Request.ClientCertificates.Add(Cert);
    Request.UserAgent = "Client Cert Sample";
    Request.Method = "GET";
    HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
    // Print the repsonse headers.
    Console.WriteLine("{0}",Response.Headers);
    Console.WriteLine();
    // Get the certificate data.
    StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.Default);
    int count;
    char [] ReadBuf = new char[1024];
    do
    {
        count = sr.Read(ReadBuf, 0, 1024);
        if (0 != count)
        {
            Console.WriteLine(new string(ReadBuf));
        }
                       
    }while(count > 0);
}
catch(Exception e)
{
    Console.WriteLine(e.Message);
}
   

//Implement the ICertificatePolicy interface.
class CertPolicy: ICertificatePolicy
{
    public bool CheckValidationResult(ServicePoint srvPoint,
X509Certificate certificate, WebRequest request, int certificateProblem)
    {
        // You can do your own certificate checking.
        // You can obtain the error values from WinError.h.

        // Return true so that any certificate will work with this sample.
        return true;
    }
}

 

使用 CryptoAPI 调用
如果您必须获取该证书从证书存储区,CryptoAPI 函数用于在获取该证书,然后将其存储在 x509 证书 类对象。 X509CertificateCollection 类枚举存储区中的所有证书,然后将其置于 X509CertificateCollection 类对象中。

如果想获得特定的证书,必须更改类代码,以使用 CertFindCertificateInStore 函数获取特定的证书。 Wincrypt.h 的文件中声明该函数。 鎴栬 € 咃,您可以枚举 X509CertificateCollection 函数,以查找所需的证书。

下面的代码示例使用从 CertEnumCertificatesInStore 函数返回集合中的第一个证书
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;

namespace SelectClientCert
{
    /// Sample that describes how how to select client cetificate and send it to the server.

    class MyCerts{

        private static int CERT_STORE_PROV_SYSTEM = 10;
        private static int CERT_SYSTEM_STORE_CURRENT_USER = (1 << 16);
        ///private static int CERT_SYSTEM_STORE_LOCAL_MACHINE = (2 << 16);

        [DllImport("CRYPT32", EntryPoint="CertOpenStore", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern IntPtr CertOpenStore(
            int storeProvider, int encodingType,
            int hcryptProv, int flags, string pvPara);

        [DllImport("CRYPT32", EntryPoint="CertEnumCertificatesInStore", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern IntPtr CertEnumCertificatesInStore(
            IntPtr storeProvider,
            IntPtr prevCertContext);

        [DllImport("CRYPT32", EntryPoint="CertCloseStore", CharSet=CharSet.Unicode, SetLastError=true)]
        public static extern bool CertCloseStore(
            IntPtr storeProvider,
            int flags);
       
        X509CertificateCollection m_certs;

        public MyCerts(){
            m_certs = new X509CertificateCollection();
        }

        public int Init()
        {
            IntPtr storeHandle;
            storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, "MY");
            IntPtr currentCertContext;
            currentCertContext = CertEnumCertificatesInStore(storeHandle, (IntPtr)0);
            int i = 0;
            while (currentCertContext != (IntPtr)0)
            {
                m_certs.Insert(i++, new X509Certificate(currentCertContext));
                currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);
            }
            CertCloseStore(storeHandle, 0);

            return m_certs.Count;
        }
       
        public X509Certificate this [int index]
        {
            get
            {
                // Check the index limits.
                if (index < 0 || index > m_certs.Count)
                    return null;
                else
                    return m_certs[index];
            }
        }
    };
    class MyHttpResource
    {
        String m_url;

        public MyHttpResource(string url){
            m_url = url;
        }

        public void GetFile(){

            HttpWebResponse  result = null;

            try{
           
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_url);
                req.Credentials  = CredentialCache.DefaultCredentials;

                ///Method1
                //req.ClientCertificates.Add(X509Certificate.CreateFromCertFile("D:\\Temp\\cert\\c1.cer"));
       
                ///Method2
                ///Uses interop services
                MyCerts mycert = new MyCerts();
                if(mycert.Init() > 0)
                    req.ClientCertificates.Add(mycert[0]);

                result = (HttpWebResponse)req.GetResponse();
               
                Stream ReceiveStream = result.GetResponseStream();
                Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

                StreamReader sr = new StreamReader( ReceiveStream, encode );
                Console.WriteLine("\r\nResponse stream received");

                Char[] read = new Char[256];
                int count = sr.Read( read, 0, 256 );

                Console.WriteLine("HTTP Response...\r\n");
                while (count > 0)
                {
                    String str = new String(read, 0, count);
                    Console.Write(str);
                    count = sr.Read(read, 0, 256);
                }

            }
            catch(WebException e)
            {
           
                Console.WriteLine("\r\nError:");
                #if (DEBUG)
                    Console.WriteLine(e.ToString());
                #else       
                    Console.WriteLine(e.Message);                
                #endif

            }
            finally
            {
                if ( result != null ) {
                    result.Close();
                }
            }
               
        }
   
    }

    class CertSample
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length < 1)
                {
                    Console.WriteLine("No url is entered to download, returning.\n");
                    Console.WriteLine("Usage: CertSample \n");
                    Console.WriteLine("  e.g: CertSample https://servername \n");

                    return;
                }

                MyHttpResource hr = new MyHttpResource(args[0]);
                hr.GetFile();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return;
        }
    }
}

 

参考有关详细信息请访问下面的 Microsoft 开发网络 (MSDN) 网站: x509 证书类http://msdn2.microsoft.com/en-us/...有关详细信息请访问下面的 Microsoft 开发网络 (MSDN)

x509 证书类
http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509certificate(vs.71).aspx)  

我们接着说,WSDL文件是得如下

代码

 ........

可是我们想一下这是一个Xml文件,我们应该怎么使用他呢,如果 是Webservce相信大家都 知道 怎么使用,直接添加引用就可以,但这里是一个Wsdl的XMl文件,我们得把他转化成一个类文件才行,

方法很简单,这是在我知道 了之后才这样讲的,呵呵

利用wsdl.exe生成webservice代理类:

根据提供的wsdl生成webservice代理类

1、开始->程序->Visual Studio 2008 命令提示

2、输入如下红色标记部分

D:\Program Files\Microsoft Visual Studio 8\VC>wsdl /language:c# /n:TestDemo /out:d:\text\TestService.cs D:\text\TestService.wsdl

在d:/text下就会产生一个TestService.cs 文件

注意:D:\text\TestService.wsdl 是wsdl路径,可以是url路径:https://www.cnblogs.com/sufei/?wsdl

如果 你想知道WSDL文件是怎么使用的话,直接写WSDL回车就可以,会出显所有的说明,呵呵

 

 

还有一个方法更方便

首先打开Visual Studio 2008,选择菜单"工具"-"外部工具"打开外部工具对话框,如图

,单击“添加”按钮添加新工具,然后在“标题”行中输入"WSDL生成代理类","命令"行中输入"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\wsdl.exe"(wsdl.exe文件的路径),"参数"行中输入"/l:cs  /out:", 单击"初始目录"行右边的三角按钮选择"项目录",勾选"使用输出窗口"和"提示输入参数",然后确定保存。

     再打开菜单"工具"可以看到多了一个"WSDL生成代理类"菜单,这时先选定一个存放生成的代理类的文件夹(必须位于并且包含于当前解决方案中),然后单击"WSDL生成代理类"菜单,弹出如下对话框,然后你只需在"/l:cs  /out:"后面空一格(必须空一格)再粘贴WebService文件的http地址如https://www.cnblogs.com/sufei?wsdl,单击"确定"看看发生了什么?是的,输出窗口会显示生成了一个类及其存放的位置,看看是不是你选定的文件夹,找到这个路径看看是不是有一个类,你会发现这个类跟上面使用命令行生成的类一模一样,个人觉得这样操作起来更简单一点。

 

 这段是来自己http://blog.sina.com.cn/s/blog_48964b120100fz14.html 在这里谢谢了

 

到这里我们应该好办多了,生成的类文件里会包括 方法使用和怎么样和服务器沟通,我们只要调用 方法就要可以了,

类文件如下

 

 

 

代码
//------------------------------------------------------------------------------
//
//     此代码由工具生成。
//     运行库版本:2.0.50727.1873
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
//

//------------------------------------------------------------------------------

//
// 此源代码由 wsdl 自动生成, Version=2.0.50727.1432。
//
namespace WSDLServices
{
    using System.Diagnostics;
    using System.Web.Services;
    using System.ComponentModel;
    using System.Web.Services.Protocols;
    using System;
    using System.Xml.Serialization;
   
   
    ///
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.1432")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="nciicGetConditionHttpBinding", Namespace="https://http://www.cnblogs.com/sufei/nciicGetCondition")]
    public partial class nciicGetCondition : System.Web.Services.Protocols.SoapHttpClientProtocol {
       
        private System.Threading.SendOrPostCallback nciicDiscernOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicCheckChinaOperationCompleted;
       
        private System.Threading.SendOrPostCallback CallnciicGetConditionOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicExactSearchOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicCourtOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicBirthplaceCompareOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicAddrExactSearchOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicCheckOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicCompareOperationCompleted;
       
        private System.Threading.SendOrPostCallback nciicCombineSearchOperationCompleted;
       
        ///
        public nciicGetCondition() {
            this.Url = "http://http://www.cnblogs.com/sufei/nciic_ws/services/nciicGetCondition";
        }
        
       .....}
}

 

 

有了这个类,最后一步就是怎么样来实现调取数据了,

 

代码
 try
            {
                //授权文件内容建议以后放在数据库或是文件里而且要加密
                string inliance = @"文件内容";

                //生成的WSDL类()
                nciicGetCondition objText = new nciicGetCondition();

                //基础URL建议加密和写在文件中
                objText.Url = "https://www.cnblogs.com/sufei";

                //编码
                objText.RequestEncoding =  Encoding.UTF8;

                //创建证书文件
                X509Certificate objx509 = new X509Certificate(Application.StartupPath + "\\tingting.cer");
               
                //证书
                objText.ClientCertificates.Add(objx509);

                //方式可有可无
                objText.UserAgent = "Client Cert Sample";

                //读XML文件
                string inConditions = File.ReadAllText(Application.StartupPath + "\\XMLFile1.xml");

                //Text文件
                this.richTextBox1.Text = objText.nciicCheck(inliance, inConditions);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 

 

 

到这里我们已经和服务器沟通上了,把回的结果集的格式 跟文档中的一样,我们自己去解析就行了,这里为了保证公司的利益就不再多写了。

结果取出来如下:             

 

代码
 //转图片的方法
        public Image Base64ToImage(string base64String)
        {
            byte[] imageBytes = Convert.FromBase64String(base64String);
            MemoryStream ms = new MemoryStream(imageBytes, 0,
              imageBytes.Length);
            ms.Write(imageBytes, 0, imageBytes.Length);
            System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);

            return image;
        }

        //取返回的信息结果
        private void button4_Click(object sender, EventArgs e)
        {
            XmlDocument objXml = new XmlDocument();

            //文件在Bin目录下
            objXml.Load(Application.StartupPath + "\\XML.xml");

            //身份证号
            textBox1.Text = objXml.SelectSingleNode("//gmsfhm").InnerText;

            //姓名
            textBox2.Text = objXml.SelectSingleNode("//xm").InnerText;

            //身份证号是否一样
            textBox4.Text = objXml.SelectSingleNode("//result_gmsfhm").InnerText;

            //姓名是否一样
            textBox3.Text = objXml.SelectSingleNode("//result_xm").InnerText;

            //取图片
            string bas64 = objXml.SelectSingleNode("//xp").InnerText;

            //显示图片
            pictureBox1.Image = Base64ToImage(bas64);
        }
原文来自:雨枫技术教程网 http://www.fengfly.com
原文网址:http://www.fengfly.com/plus/view-171681-1.html 

在使用的过程 中我又碰到了一个很棘手的问题这是我的代码

 

 //官方查询结果xml
        [WebMethod ]
        public  OrderNciis GetNciisResult(string onName, string onId)
        {
            //获取授权文件
            string inLicense = LotteryMethods.GetContext(11);

            //生成的WSDL类()
            nciicGetCondition objText = new nciicGetCondition();

            //获取基础URL
            objText.Url = LotteryMethods.GetContext(12);

            //编码
            objText.RequestEncoding = Encoding.UTF8;

            //创建证书文件
            X509Certificate objx509 = new X509Certificate(System.Configuration.ConfigurationSettings.AppSettings["cd"].ToString().Trim());

            //证书
            objText.ClientCertificates.Add(objx509);

            //方式可有可无
            objText.UserAgent = "Client Cert Sample";

            //读XML文件
            string inConditions = OrderNciisXml(onName, onId);

            //返回查询结果XML
            OrderNciis model = new OrderNciis();
            model.onLottery = objText.nciicCheck(inLicense, inConditions);
            return model;
        }

代码应该是没有问题,在本地的Vs里测试没有问题,如果在ISS里使用时报基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系”证书验证失败

城刚开始我还以为是文章路径的问题,结果不是,

接着找,后来认为是IIS里配置问题,还是不对,后来,看到  网上也有很多这的问题,但是没有真正解决的,一般都 是在说怎么生成代理类,还有带有证书的过程

 

我想了想,还是自己想办法解决吧,我把代理类分析了一下才知道 ,原来证书是在代理类里验证的,验证后会返回一个值 表示是否通过,这就好看了,

我们定义一个方法

private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error){
            // trust any certificate!!!
            System.Console.WriteLine("Warning, trust any certificate");
            //为了通过证书验证,总是返回true
            return true;
    }

只要这个方法返回为True不就完了吗?呵呵

还需要几个命名空间

 

using System.Net;
    using System.Net.Security;
    using System.Security.Authentication;
    using System.Security.Cryptography.X509Certificates;

 

 

这个应该在什么时候调用呢,当然 是在构造器里合适些

 ///
        public nciicGetCondition()
        {

            //验证服务器证书回调自动验证
            ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;
        }

 

原文来自:雨枫技术教程网 http://www.fengfly.com
原文网址:http://www.fengfly.com/plus/view-172302-1.html

转载于:https://www.cnblogs.com/zhaoyong/archive/2010/06/01/1749477.html

你可能感兴趣的:(全國身份證查詢系統nciis)