三、ADSI编程模型 了解了ADSI的基本结构之后,现在我们再讨论ADSI的编程模型。首先,我们介绍目录对象的路径表示法,通常我们使用URL路径表示法,如下: <Namespace>:或者 <Namespace>://<Provider-specific stuff> 如果我们不知道名字空间的符号表示,可以从ADSI的最基础的根“ADS:”开始,比如,我们在NT 4.0下对“ADS:”进行枚举可以得到5个提供者名字空间。 当创建目录入口(DirectoryEntry)组件的实例时,需要指定所使用的服务提供程序的类型以及要操作的对象,并与Active Directory连接,这个过程称为“绑定”。这时,你需要指定所使用的协议(LADP、WinNT等),即服务提供程序指示符。当前的一些服务提供程序包括 Internet 信息服务 (IIS)、轻量目录访问协议 (LDAP)和 WinNT等。随后你可能需要指定计算机所在的域名、组名以及计算机名,如果没有显示式指定域名(服务器名),系统就会在整个域中查找与用户绑定过程相关的域控制器,并且使用所找到的第一个域控制器。这样,WinNT下的绑定语法大致为: 1.WinNT://MyDomain/Group/ 2.WinNT://MyDomain/MyComputer/aPrinter/ 这个过程中,可能需要指定路径信息,Path属性唯一地标识网络环境中的路径信息,设置该属性将从目录存储区检索新项,它不更改当前绑定的项的路径。同绑定类似,Path 属性的语法取决于服务提供程序。 在WinNT 下连接到计算机上的组语法为:WinNT://domain/computer/group;而IIS下连接到Web目录则为:IIS://LocalHost/W3SVC/1/ROOT/web-directory-name。 LDAP对应的路径如下所示: LDAP://<Server_Name>/cn=<User_Name>,cn=Recipients,ou=<Site_Name>,o=<Organization_Name> 若要使用 ADSI 技术,在客户端计算机上必须提供有ADSI SDK或ADSI运行库,这可以通过安装 ADSI 2.5 或更高版本来实现。对于 Windows NT 5.0 版、Windows 2000 或 Windows XP,默认安装了 ADSI 2.5。如果使用的是以前版本的 Windows,则您可以自己从 Microsoft Web 站点安装该 SDK。 注册表中ADSI信息如下: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Active Setup\Installed Components\{E92B03AB-B707-11d2-9CBD-0000F87A369E}(下图左) HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ADs(下图右) Windows系统目录下C:\WINDOWS\system32中对应ADSI接口的DLL包括: ActiveDs.dll: ADS Router Layer DLL ActiveDs.tlb: Type Liberary Adsiisex.dll: ADSI Extension Adsiis.dll: ADs IIS Provider DLL Adsldp.dll: ADs LDAP Provider DLL Adsldpc.dll: ADs LDAP Provider C DLL Adsmsext.dll: ADs LDAP Provider DLL Adsnds.dll: ADs NDS Provider DLL Adsnt.dll: ADs Windows NT Provider DLL Adsnw.dll: ADs Netware 3.12 Provider DLL 在Visual C++中使用ADSI,需要#include <activeds.h>,链接activeds.lib和adsiid.lib库文件,并要使用Unicode字符串,涉及<Iads.h> 和<Adshlp.h> 。 Win32中ADSI COM接口参考: 《ADSI Interface》 http://msdn.microsoft.com/en-us/library/aa772202(VS.85).aspx .NET框架下,System.DirectoryServices命名空间中的DirectoryEntry提供了活动目录组件管理功能,它调用ADSI操作IIS。 以下C#代码调用.NET的System.DirectoryServices.DirectoryEntry,访问WinNT枚举局域网内的所有主机。 using System; using System.Collections.Generic; using System.DirectoryServices; // 添加引用 using System.Text; namespace NT { class Program { static void Main() { EnumComputers(); } static void EnumComputers() { using(DirectoryEntry root = new DirectoryEntry("WinNT:")) { foreach(DirectoryEntry domain in root.Children) { Console.WriteLine("Domain | WorkGroup:\t"+domain.Name); foreach(DirectoryEntry computer in domain.Children) { Console.WriteLine("Computer:\t"+computer.Name); } } } } } } 参考: 《AdminScripts脚本批量生成虚拟目录》 http://www.cnblogs.com/skylaugh/archive/2007/05/18/751029.html 《VC创建IIS虚拟目录》 http://topic.csdn.net/t/20030224/15/1460500.html 《用VC控制IIS属性》 http://www.wangchao.net.cn/bbsdetail_556648.html 《.NET如何操作IIS(原理篇)》 http://www.host01.com/article/Net/00020002/0561212543337508.htm 《浅析.Net下Active Directory编程技术》 http://industry.ccidnet.com/art/322/20021025/28703_1.html 《Build an IIS Virtual Directory Addin for Visual Studio.NET》 http://www.eggheadcafe.com/articles/20040112.asp 《IIS虚拟目录的创建及管理》 http://www.chinageren.com/chengxu/ASP_NET/chengxu_29801.html 《使用 DirectoryEntry实现IIS虚拟目录的管理》 http://www.cnblogs.com/libiyang/archive/2008/11/12/294455.html

评论