zoukankan      html  css  js  c++  java
  • 让WPF Browser Application自动下载并安装数字证书(转)

    原文地址:http://hi.csdn.net/link.php?url=http://blog.csdn.net%2Fstarlee

    在我的那篇《给WPF Browser Application创建数字证书》一文中,我提供了一个给WPF Browser Application创建数字证书的方法,从而避免出现“Trust Not Granted”的错误,而使它成为一个真正的“full trust”的WPF Browser Application。
        可是为了在客户端电脑上用浏览器访问这个WPF Browser Application,就必须在客户端电脑上安装数字证书。要是有很多客户端访问的话,他们都要自己去下载和安装数字证书。为此,就必须给用户提供一个可供下载的数字证书和数字证书的安装方法。可以说,是给用户带来了很多不便。
        下面就提供一种让客户端用浏览器访WPF Browser Application时自动下载并安装数字证书的方法。
        这个方法包含两个大步骤:首先,是写一个下载并安装数字证书的程序;然后,是将这个程序包含到WPF Browser Application的安装程序中。
        下载并安装数字证书的程序比较简单,只要用C#创建一个Console Application,然后添加如下的代码就行了。(本例中创建的是一个名为CertificateInstaller的控制台程序)

    using System;  
    using System.Net;  
    using System.Windows.Forms;  
    using System.Security.Cryptography.X509Certificates;  
    namespace CertificateInstaller  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                try 
                {  
                    if (args.Length > 0)  
                    {  
                        WebClient webclient = new WebClient();  
                        byte[] certificatefile = webclient.DownloadData(args[0]);  
                        if (certificatefile.Length > 0)  
                        {  
                            InstallCertificate(StoreName.AuthRoot, certificatefile);  
                            InstallCertificate(StoreName.TrustedPublisher, certificatefile);  
                        }  
                        else 
                        {  
                            ShowErrorMessage("Can't download the certificate file.");  
                        }  
                    }  
                }  
                catch (System.Exception ex)  
                {  
                    ShowErrorMessage(ex.Message);  
                }  
            }  
            static void InstallCertificate(StoreName storageName, byte[] certificatefile)  
            {  
                X509Certificate2 certificate = new X509Certificate2(certificatefile);  
                X509Store store = new X509Store(storageName, StoreLocation.LocalMachine);  
                store.Open(OpenFlags.ReadWrite);  
                store.Remove(certificate);  
                store.Add(certificate);  
                store.Close();  
            }  
            static void ShowErrorMessage(string strErrorMessage)  
            {  
                MessageBox.Show(strErrorMessage, "Certificate Installation Error", MessageBoxButtons.OK, MessageBoxIcon.Error);  
            }  
        }  
    }

    下面是将编译好的CertificateInstaller.exe添加到WPF Browser Application的安装程序中的步骤。
        1. 到目录C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\下找到WindowsInstaller3_1文件夹。
        2. 拷贝并粘贴WindowsInstaller3_1文件夹,并重命名为CertificateInstaller。
        3. 将CertificateInstaller.exe拷贝到C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bootstrapper\Packages\CertificateInstaller文件夹下。
        4. 将该文件夹中的Product.xml改成如下的内容。

    <?xml version="1.0" encoding="utf-8" ?> 
    <Product 
      xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper"
      ProductCode="CertificateInsteraller" 

        <PackageFiles CopyAllPackageFiles="true"> 
            <PackageFile Name="CertificateInstaller.exe" /> 
        </PackageFiles> 
        <Commands Reboot="Immediate"> 
            <Command PackageFile="CertificateInstaller.exe" 
                     Arguments= 'http://XXX/xxx.cer'
                     EstimatedInstallSeconds="30" > 
                <ExitCodes> 
                    <ExitCode Value="0" Result="Success"/> 
                    <ExitCode Value="1641" Result="SuccessReboot"/> 
                    <ExitCode Value="3010" Result="SuccessReboot"/> 
                    <DefaultExitCode Result="Fail" FormatMessageFromSystem="true" String="GeneralFailure" /> 
                </ExitCodes> 
            </Command> 
        </Commands> 
    </Product> 
    <?xml version="1.0" encoding="utf-8" ?>

    <Product
      xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper"
      ProductCode="CertificateInsteraller"
    >

        <PackageFiles CopyAllPackageFiles="true">
            <PackageFile Name="CertificateInstaller.exe" />
        </PackageFiles>

        <Commands Reboot="Immediate">
            <Command PackageFile="CertificateInstaller.exe"
                     Arguments= 'http://XXX/xxx.cer'
                     EstimatedInstallSeconds="30" >
                <ExitCodes>
                    <ExitCode Value="0" Result="Success"/>
                    <ExitCode Value="1641" Result="SuccessReboot"/>
                    <ExitCode Value="3010" Result="SuccessReboot"/>
                    <DefaultExitCode Result="Fail" FormatMessageFromSystem="true" String="GeneralFailure" />
                </ExitCodes>
            </Command>
        </Commands>
    </Product>
        里面的“http://XXX/xxx.cer”就是可供下载的数字证书的链接(可以跟WPF Browser Application放到同一个Web Server上,这样管理起来方便一些。当然,也可以放在任何一个用户可以访问的Web Server上)。
        5. 进入“en”文件夹,并将Package.xml改成如下的内容。

    <?xml version="1.0" encoding="utf-8" ?> 
    <Package 
      xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper"
      Name="DisplayName" 
      Culture="Culture" 

        <Strings> 
            <String Name="DisplayName">My Certificate Insteraller</String> 
        </Strings> 
    </Package> 
    <?xml version="1.0" encoding="utf-8" ?>

    <Package
      xmlns="http://schemas.microsoft.com/developer/2004/01/bootstrapper"
      Name="DisplayName"
      Culture="Culture"
    >

        <Strings>
            <String Name="DisplayName">My Certificate Insteraller</String>
        </Strings>

    </Package>

        6. 打开WPF Browser Application。
        7. 在属性面板里面选择“Publish”选项,点击“Preequisites...”按钮。

        8. 在弹出的“Preequisites”对话框中选中“Create setup program to install prerequisite components”,并在列表中找到“My Certificate Insteraler”(如果找不到的话,关闭VS再重新打开),选中它并点击“OK”按钮。

        9. 重新发布WPF Browser Application。

        下面是用户在客户端安装数字证书的步骤。
        1. 用户在客户端用浏览器访问WPF Browser Application时会出现类似下面这样的页面。

        可以看到“My Certificate Insteraler”已经被包含在安装程序中了。
        2. 点击“Run”按钮,会弹出下面的对话框。

        3. 点击“Run”按钮,并在弹出的对话框上继续点击“Run”按钮。

        4. 这个时候就会弹出安装对话框,点击“Install”按钮就会运行CertificateInstaller.exe,自动下载并安装数字证书。

      通过上面的步骤,数字证书就自动安装到了客户端电脑上了。以后用户就可以直接访问WPF Browser Application了。

  • 相关阅读:
    Redis 如何与数据库事务保持一致
    QQ、微信 唯一登陆设计
    Node.js Sequelize如何实现数据库的读写分离
    node.js web应用优化之读写分离
    在docker中使用mysql数据库,在局域网访问
    docker常用管理命令
    基于 Egg.js 框架的 Node.js 服务构建之用户管理设计
    使用mousedown、mousemove、mouseup实现拖拽效果
    VUE中事件修饰符:stop prevent self capture
    基于jsplumb插件制作可拖拽、保存流程图、重绘保存后的流程图总结
  • 原文地址:https://www.cnblogs.com/jacle169/p/2810801.html
Copyright © 2011-2022 走看看