部署访问注册表和MAC地址的程序

       前一段时间又在做部署,由于程序(B/S)上有一些变动,在起始页面要访问当前Server的注册表和MAC地址。程序在调试过程中没有发现什么问题,但是部署后却出现了注册表和MAC地址不可访问的问题。记得我在写《安装部署中的数据库打包和快捷方式启动浏览器》之后,曾经有一个朋友问过我这个类似的问题。当时由于我没有接触到所以也没能给他很好的解决。不过这次,天上的石头终于落到了我的头上。我也遇到了这个问题。

       当时我只是觉得问题出现在了访问的权限上,为了访问注册表和MAC地址,我们在Web.config中加入了一个模拟用户:

       <identity impersonate="true" />

       但实际上,这个模拟用户在调试程序时使用是没有问题的。当我部署到其他的机器上,由于这个用户的权限不足,所以程序无法访问本地资源。

       后来,我们修改了这个节点

       <identity impersonate="true" userName="UserName" password="PassWord"/>

       其中UserNamePassWord为当前机器的用户名密码,部署的程序便可以访问本地资源。

       随着这个问题的解决,新的问题又出现了。有些单位的Server为了提高安全性,密码是常常变化的,这样,这个程序就又用不了了。于是决定在部署的时候,程序中加一个WindowFrom的程序。当Server的密码变化了,就用这个程序来改变Web.config中的脚本。如下图:





代码如下:

 

string Path = ConfigurationSettings.AppSettings["Path"].ToString();//在配置文件中获取Web.config的地址

                            FileInfo file = new FileInfo(Path);

                            XmlDocument doc = new XmlDocument();

                            doc.Load(file.FullName);

                            XmlElement root = doc.DocumentElement;

                            XmlNodeList list = root.SelectNodes("/configuration/system.web/identity");

                            //<!--identity impersonate="true" userName="esint\shiwei.li" password=""/-->

                            if(list.Count != 0)

                            {

                                   XmlNode node = (XmlNode)list[0];

                                   node.Attributes["impersonate"].Value = "true";

                                   node.Attributes["userName"].Value = this.txtUserName.Text;

                                   node.Attributes["password"].Value = this.txtPassword.Text;

                                   doc.Save(file.FullName);

                            }

       其中Path参数是WinForm配置文件中的一个参数。记录了Web.Config文件的路径。

                     <add key="Path" value=""/>

    这个参数在安装部署时进行修改,这样就要加一个用户自定义的操作,自定义操作中的代码如下:

    FileInfo file = new FileInfo(this.Context.Parameters["targetdir"] + @"\ActiveProgramme\ActivePrograme.exe.config");

    XmlDocument doc = new XmlDocument();

    doc.Load(file.FullName);

    XmlElement root = doc.DocumentElement;

    XmlNodeList list = root.SelectNodes("/configuration/appSettings/add");

    foreach(XmlNode node in list)

    {

        if(node.Attributes["key"].Value == "Path")

        {

            node.Attributes["value"].Value = this.Context.Parameters["targetdir"].ToString() + @"\Web.config";

            doc.Save(file.FullName);

            break;

        }

    }

    其中"\ActiveProgramme\ActivePrograme.exe.config"WinForm程序中的配置文件的路径,ActivePrograme.exe.config是在WinForm下的App.config在安装之后的文件,就是程序集名称+.exe.config

    以上是我对这个问题的解决方案,当作完后回头看看好像绕了很大一个圈。先在部署的时候记录Web.Config文件的绝对位置,然后在客户Server密码变动时修改Web.config中的节点属性。而且,总觉得这样降低了安全性。但也不知道有什么更好的方法。希望大家能多多给我提些建议。

posted @ 2006-11-16 13:54  KiddLee  Views(2040)  Comments(9Edit  收藏  举报