C #2.0 simple AD operations

Source: Internet
Author: User
Tags ldap protocol

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:

  1. Create and bind an LDAP connection:

    NetworkCredentialCredential =New NetworkCredential("Administrator", "password ");

    LdapConnectionLdapConnection =New LdapConnection("192.168.0.6 ");

    LdapConnection. Credential = credential;

    LdapConnection. Bind ();

  2. 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 );

  3. 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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.