System.DirectoryServices
 
System.DirectoryServices 命名空间用以从托管代码简便地访问 Active Directory。它使用 Active Directory 服务接口 (ADSI) 技术。可参考:
http://msdn.microsoft.com/zh-cn/library/system.directoryservices(v=VS.80).aspx

其中DirectoryEntry Class封装 Active Directory 层次结构中的节点或对象。使用此类可读取或更新属性等。可参考:
http://msdn.microsoft.com/zh-cn/library/system.directoryservices.directoryentry(VS.80).aspx
 
在我们公司的项目中,准许我们的合作伙伴修改自己的密码,我们合作伙伴的用户都在我们的AD中创建的。

我们做好了修改密码的程序,先在本地进行了测试,成功。
部署到了服务器上,这台服务器没有加入域,再次测试时却报错了,“未知的用户名密码”。经过搜索,了解了一些原理。
 
“And according to your ASP.NET application, it works when hosted in VS test
server but failed when hosting in IIS(on XP). That means, when hosting in
VS test server, since test server running under your current logon user(I
assume it a domain user account), it can correctly login the domain. While
running in IIS, the default ASP.NET worker process identity is
machine\ASPNET(local account) that can not login domain, that's why the
exception occurs.
大致的意思是说,用开发环境测试时,用的是本地当前用户的权限,把程序放到服务器上,修改密码时用的是ASPNET的权限,这个用户没有权限修改AD的密码就导致了异常。
所以我们把焦点放到DirectoryEntry 上;
DirectoryEntry 有四个构造函数,说白了就是这个类实例化的时候可以实例化四种,我们刚才使用的是默认的那种,执行修改密码的方法是默认使用了IIS的用户,所以导致了修改密码的失败。
我们可以使用第四种构造函数,
'用法
Dim path As String
Dim username As String
Dim password As String
Dim authenticationType As AuthenticationTypes
Dim instance As New DirectoryEntry(path, username, password, authenticationType)
参数
path;
username:在对客户端进行身份验证时使用的用户名。Username 属性初始化为该值。
password;
authenticationType:AuthenticationTypes 值之一。AuthenticationType 属性初始化为该值。
这样,我们就可以指定具有权限的用户名来执行这段代码。例如:
DirectoryEntry root = new DirectoryEntry(path, "域\\有权限修改AD密码权限的用户名", "用户密码", AuthenticationTypes.Secure);
 
把修改好的代码服务器,测试结果,成功,OK!