How to use C # to add, delete, modify, and query users and organizational units in Active Directory)

Source: Internet
Author: User
Tags samaccountname

Http://www.pc123.org/html/net/asp_net/200806/16_3101.html

 

First, let's take a look at what is active directory. I don't need to describe it. I can refer to the following URL or search for the Active Directory keyword in the. NET Help document.

Next, let's look at the permissions. You can use "network neighbor -- entire network -- directory -- Demain (your domain name)" to view all information about the domain. You can see what is going on at a glance.
Users in all organizational units are in the users (container) -- demain users (group ).
UseCodeIf you are a domain administrator, you can perform any operations. Otherwise, you can only query user attributes.

Private void searchuser ()
{
String domainname = "Domain ";
String groupname = "Domain Users ";
String dirmemname = "";
// Obtain each user name in the Domain Users domain user
System. directoryservices. directoryentry group = new system. directoryservices. directoryentry ("winnt: //" + domainname + "/" + groupname + ", group ");
Foreach (Object member in (ienumerable) group. Invoke ("members "))
{
// Based on a large number of users, for example, "LDAP: // ou = software package, ou = System Development Department, ou = Information Service Department, ou = Operation Support Center, ou = XX Company, Dc = domain, Dc = com, Dc = cn"
System. directoryservices. directoryentry dirmem = new system. directoryservices. directoryentry (member );
Dirmemname = dirmem. Name;
String domainname = "Domain ";
String filterstr = "(samaccountname =" + dirmemname + ")";
System. directoryservices. directorysearcher findimethyl = new system. directoryservices. directorysearcher (domainname );
Findimethyl. Filter = filterstr;
System. directoryservices. searchresult findres = findimethyl. findone ();
System. directoryservices. directoryentry myuser = findres. getdirectoryentry ();
String oupath = myuser. Parent. path;
// Find the LDAP address of the user, log on to the domain administrator, and obtain the attributes of the user.
String strfieldsvalue = "", strfields = "";
System. directoryservices. directoryentry myds = new system. directoryservices. directoryentry (oupath, "Domain Administrator name", "domain administrator password ");
Foreach (system. directoryservices. directoryentry tempentry in myds. Children)
{
If (tempentry. schemaclassname. tostring () = "user" & tempentry. properties ["samaccountname"]. value. tostring (). tolower () = dirmemname)
{
Foreach (string propertyname in tempentry. properties. propertynames)
{
String onenode = propertyname + ":" +
Entry. properties [propertyname] [0]. tostring ();
This. textbox1.text = onenode;
}
}

-------------------------------------------

Public void adduser (string strpath, string username, string chinesename) // the organization to which the user is added by strpath, for example, "LDAP: // ou = XX Company, Dc = domain, dc = com "account, Chinese name {
Try
{
String rootdse;
// System. directoryservices. directorysearcher dsesearcher = new system. directoryservices. directorysearcher ();
// Rootdse = dsesearcher. searchroot. path;
// Rootdse = "LDAP: // dc = domain, Dc = com ";
// Rootdse = rootdse. insert (7, "cn = users ,");
System. directoryservices. directoryentry myde = new system. directoryservices. directoryentry (strpath );
System. directoryservices. directoryentries myentries = myde. Children;
// Create a new entry 'sample' in the container.
String strname = "cn =" + chinesename;
System. directoryservices. directoryentry mydirectoryentry = myentries. Add (strname, "user ");

// MessageBox. Show (mydirectoryentry. schemaclassname. tostring ());
Mydirectoryentry. properties ["userprincipalname"]. value = username;
Mydirectoryentry. properties ["name"]. value = chinesename;
Mydirectoryentry. properties ["samaccountname"]. value = username;
Mydirectoryentry. properties ["useraccountcontrol"]. value = 66048; // 590336;
Mydirectoryentry. commitchanges ();
}

----------------------------------------------

Private void Addou (string strpath, string ouname) // Add the organizational unit to the organizational unit of the strpath. The organizational unit name.
{
Try
{
// String rootdse;
// System. directoryservices. directorysearcher dsesearcher = new system. directoryservices. directorysearcher ();
// Rootdse = dsesearcher. searchroot. path;
// Rootdse = "LDAP: // ou = Baiyi Fashion Plaza, Dc = domain, Dc = com ";

System. directoryservices. directoryentry myde = new system. directoryservices. directoryentry (strpath );
System. directoryservices. directoryentries myentries = myde. Children;
String name = "ou =" + ouname;
System. directoryservices. directoryentry mydirectoryentry = myentries. Add (name, "organizationalunit ");

Mydirectoryentry. properties ["name"]. value = ouname;
Mydirectoryentry. properties ["instancetype"]. value = 4;
Mydirectoryentry. properties ["distinguishedname"]. value = "ou =" + ouname + ", Dc = domain, Dc = com )";
Mydirectoryentry. properties ["objectcategory"]. value = "cn = organizational-unit, Cn = schema, Cn = configuration, Dc = sedep, Dc = com ";
Mydirectoryentry. properties ["ou"]. value = ouname;
Mydirectoryentry. properties ["postalcode"]. value = "777 ";

Mydirectoryentry. commitchanges ();
// Usermoveto ("LDAP: // ou =" + ouname + ", Dc = sedep, Dc = com", strpath );
}
Catch (exception raiseerr)

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.