转载自:http://blog.joycode.com/liuhuimiao/articles/30654.aspx
System.DirectoryServices.Protocols.dll是.NET2.0新增加的一个针对目录服务访问协议处理的组件,其下只有一个System.DirectoryServices.Protocols命名空间。在该命名空间下,主要有LDAP、DSML两种国际标准协议的一系列实现类。通过这些类,完全可以很方便地实现对目录的操作管理,这个实现步骤就有点类似你利用ADO.NET操作数据库一样方便。
在System.DirectoryServices.Protocols命名空间里,主要有这样几个类:LdapConnection(LDAP协议方式的目录连接类,负责创建LDAP连接并绑定LDAP服务器)、DsmlSoapHttpConnection(DSML协议方式的目录连接类、负责创建DSML连接并绑定DSML服务器)、AddRequest/AddResponse、ModifyRequest/ModifyResponse、ModifyDNRequest/ModifyDNResponse、CompareRequest/CompareResponse、SearchRequest/SearchResponse、DeleteRequest/DeleteResponse、DsmlRequestDocument/DsmlResponseDocument。这些类在实际编程应用中的关系如下图:
用户利用LdapConnection/DsmlSoapHttpConnection跟LDAP服务器/DSML服务器建立连接并绑定后,即可创建一系列相应的操作请求(如增加一新对象请求AddRequest),然后通过连接对象的SendRequest方法把请求命令发送到服务器,服务器根据请求进行相应处理后,把应答信息传回给客户端。需要指出的是,对于DSML方式的请求,还可以利用DsmlRequestDocument将AddRequest、ModifyRequest、ModifyDNRequest、CompareRequet、SearchRequest和DeleteRequest的任意几个请求组合组装起来,一并发送到DSML服务器进行处理。
LdapConnection的使用:
NetworkCredential credential = new NetworkCredential("Administrator", "password");
LdapConnection ldapConnection = new LdapConnection("192.168.0.6");
ldapConnection.Credential = credential;
ldapConnection.Bind();
string targetDN = "DC=mydomain,DC=local";
// 增加一个名为MyOU的组织单元
string ou = "OU=MyOU," + targetDN;
string objectClass = "organizationalUnit";
AddRequest addRequest = new AddRequest(ou, objectClass);
ldapConnection.SendRequest(addRequest);
执行完SendRequest()后,如果没有出现异常,那么MyOU已经成功增加了。当然,如果你还需要进一步对SendRequest()操作后的应答信息进行处理的话,也可以类似下面这样写,其中将在屏幕上输出“Success“的结果码:
AddResponse addResponse = (AddResponse)ldapConnection.SendRequest(addRequest);
Console.WriteLine(addResponse.ResultCode.ToString());
至此,一个LDAP请求已经处理完毕。上面的完整代码可以点这里进行查看。
using System;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
NetworkCredential credential = new NetworkCredential("Administrator", "password");
LdapConnection ldapConnection = new LdapConnection("192.168.0.6");
ldapConnection.Credential = credential;
ldapConnection.Bind();
string targetDN = "DC=mydomain,DC=local";
//增加一个名为MyOU的OU
string ou = "OU=MyOU," + targetDN;
string objectClass = "organizationalUnit";
AddRequest addRequest = new AddRequest(ou, objectClass);
AddResponse addResponse = (AddResponse)ldapConnection.SendRequest(addRequest);
Console.WriteLine(addResponse.ResultCode.ToString());
|
类似上面增加操作,还可以利用DeleteRequest进行删除操作、ModifyDNRequest进行重命名或移动操作、ModifyRequest进行修改对象属性操作、SearchRequest进行查询操作、CompareRequest进行验证比较操作。
DsmlSoapHttpConnection的使用:
NetworkCredential credential = new NetworkCredential("Administrator", "password");
Uri dsmlServerUri = new Uri("http://192.168.0.6/dsml/adssoap.dsmlx");
DsmlSoapHttpConnection dsmlSoapHttpConnection = new DsmlSoapHttpConnection(dsmlServerUri);
dsmlSoapHttpConnection.Credential = credential;
string targetDN = "DC=mydomain,DC=local";
//增加一个名为MyOU的OU
string ou = "OU=MyOU," + targetDN;
string objectClass = "organizationalUnit";
AddRequest addRequest = new AddRequest(ou, objectClass);
DsmlResponseDocument dsmlResponseDocument = dsmlSoapHttpConnection.SendRequest(addRequest);
Console.WriteLine(dsmlResponseDocument[0].ResultCode.ToString());
可以看到,DsmlResponseDocument可以包含多个应答信息,应该还需要指定下标,才能得到具体返回的结果码。
至此,一个DSML请求已经处理完毕。上面的完整代码可以点这里进行查看。
using System;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
NetworkCredential credential = new NetworkCredential("Administrator", "password");
Uri dsmlServerUri = new Uri("http://192.168.0.6/dsml/adssoap.dsmlx");
DsmlSoapHttpConnection dsmlSoapHttpConnection = new DsmlSoapHttpConnection(dsmlServerUri);
dsmlSoapHttpConnection.Credential = credential;
string targetDN = "DC=mydomain,DC=local";
//增加一个名为MyOU的OU
string ou = "OU=MyOU," + targetDN;
string objectClass = "organizationalUnit";
AddRequest addRequest = new AddRequest(ou, objectClass);
DsmlResponseDocument dsmlResponseDocument = dsmlSoapHttpConnection.SendRequest(addRequest);
Console.WriteLine(dsmlResponseDocument[0].ResultCode.ToString());
|
另外,对于DSML的多条操作请求一起发送的情况,可以创建DsmlRequestDocument对象去包含各种操作请求,具体可以参考这里的代码。
using System;
using System.Net;
using System.DirectoryServices;
using System.DirectoryServices.Protocols;
NetworkCredential credential = new NetworkCredential("Administrator", "password");
ri dsmlServerUri = new Uri("http://192.168.0.6/dsml/adssoap.dsmlx");
DsmlSoapHttpConnection dsmlSoapHttpConnection = new DsmlSoapHttpConnection(dsmlServerUri);
dsmlSoapHttpConnection.Credential = credential;
string targetDN = "DC=mydomain,DC=local";
string ou = "OU=MyOU," + targetDN;
string objectClass = "organizationalUnit";
DsmlRequestDocument batchRequest = new DsmlRequestDocument();
AddRequest addRequest;
ModifyRequest modifyRequest;
addRequest = new AddRequest(ou, objectClass);
addRequest.RequestId = "Add1";
batchRequest.Add(addRequest);
modifyRequest = new ModifyRequest(ou, "description", new string[]{"This is description of MyOU"}, DirectoryAttributeOperation.Replace);
modifyRequest.RequestId = "Modify1";
batchRequest.Add(modifyRequest);
DsmlResponseDocument batchResponse = dsmlSoapHttpConnection.SendRequest(batchRequest);
foreach (DirectoryResponse response in batchResponse)
{
Console.WriteLine(response.GetType().Name + ": \tId=" + response.RequestId + ",\tResultCode=" + response.ResultCode);
}
|
感觉怎样?我想肯定舒服了很多,毕竟这样的编程逻辑对我们来说,是再熟悉也不错的了。另外,对于目录这块,.NET2.0还在System.DirectoryServices.dll组件里也增加了一个新的命名空间System.DirectoryServices.ActiveDirectory。顾名思义,该命名空间完成的功能就是对活动目录进行更完整的处理操作,比如对域林、域树、域、域控制器、目录复制、活动目录架构、域信任等的操作,让你尽可能完全在纯托管的代码中实现对活动目录的操作。