System. DirectoryServices. Protocols. dll is a newly added component of. NET2.0 for directory service access protocol processing. It has only one System. DirectoryServices. Protocols namespace. In this namespace, there are a series of implementation classes for LDAP and DSML international standard protocols. Using these classes, you can easily manage directory operations. This implementation step is just as convenient as using ADO. NET to operate databases.
In System. directoryServices. the Protocols namespace mainly has the following classes: LdapConnection (directory connection class in LDAP protocol mode, which is responsible for creating LDAP connections and binding LDAP servers) dsmlSoapHttpConnection (directory connection class in the DSML protocol mode, responsible for creating DSML connections and binding DSML servers), AddRequest/AddResponse, ModifyRequest/ModifyResponse, ModifyDNRequest/ModifyDNResponse, CompareRequest/CompareResponse, SearchRequest/SearchResponse, DeleteRequest/response, DsmlRequestDocument/DsmlResponseDocument. The relationships between these classes in actual programming applications are as follows:
After you use LdapConnection/DsmlSoapHttpConnection to establish a connection with the LDAP server/DSML server and bind it, you can create a series of corresponding operation requests (such as adding a new object request AddRequest ), then, the Request command is sent to the server through the SendRequest method of the connection object. After the server processes the request accordingly, it returns the response information to the client. It should be noted that for DSML requests, DsmlRequestDocument can also be used to assemble any of the requests of AddRequest, ModifyRequest, ModifyDNRequest, CompareRequet, SearchRequest, and DeleteRequest, it is sent to the DSML server for processing.
Use of LdapConnection:
- Create and bind an LDAP connection:
NetworkCredentialCredential =New NetworkCredential("Administrator", "password ");
LdapConnectionLdapConnection =New LdapConnection("192.168.0.6 ");
LdapConnection. Credential = credential;
LdapConnection. Bind ();
- Create a request to add an OU named MyOU. After MyOU is created, its DN is OU = MyOU, DC = mydomain, DC = local:
StringTargetDN = "DC = mydomain, DC = local ";
//AddMyOUOrganization Unit
StringOu = "OU = MyOU," + targetDN;
StringObjectClass = "organizationalUnit ";
AddRequestAddRequest =New AddRequest(Ou, objectClass );
- Send the request to the server for processing:
LdapConnection. SendRequest (addRequest );
After SendRequest () is executed, if no exception occurs, the MyOU has been successfully added. Of course, if you need to further process the response information after the SendRequest () operation, you can write it like the following, where the "Success" result code will be output on the screen:
AddResponseAddResponse = (AddResponse) LdapConnection. SendRequest (addRequest );