How ad uses C # for pruning, querying users and OUs

Source: Internet
Author: User

Text:

First, let's look at what'sActive Directory. Without my description, look at the following URLs, or inThe. NET self-help documentation is based onActive directory Keyword One search, it all understand.
Http://developer.ccidnet.com/pub/article/c322_a28703_p2.html

Next, let's take a look at permissions. You can pass"My Network Places--The entire network--directory--demain (your domain nameYou can see all the information about the domain, and you'll know what's going on in a rough look.
We need to tell you: all the users under the organizational unit areUsers (Container)--demain Users (group) in
With code access, if you are a domain administrator user, you can do anything else, or you will only be able to query user properties.

private void Searchuser ()
{
String domainName = "Domain";
String groupName = "Domain Users";
String Dirmemname= "";
InEach user name is obtained in Domain users
System.DirectoryServices.DirectoryEntry Group = new System.DirectoryServices.DirectoryEntry ("winnt://" + DomainName + "/" + GroupName + ", group");
foreach (Object member in (IEnumerable) group. Invoke ("members"))
{
Generated by a user such as:"ldap://ou= Package Soft body Lesson, ou= System Development Department, ou= Information Service office, Ou= Operations 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 Findme = new System.DirectoryServices.DirectorySearcher (DomainName);
Findme.filter = Filterstr;
System.DirectoryServices.SearchResult findres = Findme.findone ();
System.DirectoryServices.DirectoryEntry MyUser = Findres.getdirectoryentry ();
String Oupath=myuser.parent.path;
Locate the user that contains theLDAP: After the domain administrator logs on, and gets the properties 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;
}
}


<! [cdata[

<br>public void AddUser (String strpath,string username,string

Chinesename)//strpath add user to which organizational unit such as "Ldap://ou=xx company , dc=domain,dc=com" account number, Chinese name {

<br>try <br>{<br>string RootDSE;

<br>//system.directoryservices.directorysearcher dsesearcher= New

System.DirectoryServices.DirectorySearcher ();

<BR>//RootDSE=DSESearcher.SearchRoot.Path;

<br>//rootdse= "ldap://dc=domain,dc=com";

<br>//rootdse=rootdse.insert (7, "cn=users,");

<br>system.directoryservices.directoryentry Myde = new

System.DirectoryServices.DirectoryEntry (strpath);

<br>system.directoryservices.directoryentries myentries = Myde.children; <br>//

Create a new entry ' Sample ' in the container. <br>string

Strname= "cn=" +chinesename; <br>system.directoryservices.directoryentry

Mydirectoryentry = Myentries.add (strname, "user");

<br><br>//messagebox.show (MyDirectoryEntry.SchemaClassName.ToString ());

<br>mydirectoryentry.properties["userPrincipalName"]. Value=username;


mydirectoryentry.properties["Name"]. Value=chinesename;
mydirectoryentry.properties["sAMAccountName"]. Value=username;
mydirectoryentry.properties["userAccountControl"]. Value = 66048; 590336;
Mydirectoryentry.commitchanges ();
}


<! [cdata[

<br>private void Addou (String strpath,string ouname)//Add organization to strpath organizational unit, organization name

<br>{<br>try <br>{<br>//string RootDSE;

<br>//system.directoryservices.directorysearcher dsesearcher= New

System.DirectoryServices.DirectorySearcher ();

<BR>//RootDSE=DSESearcher.SearchRoot.Path;

<br>//rootdse= "ldap://ou= Fashion square , dc=domain,dc=com";

<br><br>system.directoryservices.directoryentry Myde = new

System.DirectoryServices.DirectoryEntry (strpath);

<br>system.directoryservices.directoryentries myentries = Myde.children;

<br>string name= "ou=" +ouname; <br>system.directoryservices.directoryentry

Mydirectoryentry = Myentries.add (name, "organizationalunit");

<br><br>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)
{
MessageBox.Show (Raiseerr.message);
}
}


<! [cdata[

<br>private void ModifyUser () <br>{<br>try <br>{<br>string

Domainname= "Domain"; <br>string filterstr = "(Samaccountname=karlluo)";

<br>system.directoryservices.directorysearcher Findme = new

System.DirectoryServices.DirectorySearcher (DomainName); <br>findme.filter =

FILTERSTR; <br>system.directoryservices.searchresult findres = Findme.findone ();

<br>string Tt=findres.path; <br>system.directoryservices.directoryentry MyUser =

Findres.getdirectoryentry (); <br>string Oupath=myuser.parent.path;

<br><br>directoryentry myds=new DirectoryEntry (Oupath, "domain administrator name ", "Domain Administrator password ");

<br><br>foreach (System.DirectoryServices.DirectoryEntry tempentry in

myDS. Children) <br>{<br>if (tempEntry.SchemaClassName.ToString () = = "User")

<br>{

<br>if (tempentry.properties["sAMAccountName"). Value.tostring (). ToLower () = = "Karlluo")


{
Tempentry.usepropertycache=true;
tempentry.properties["St"]. Value= "YYYYYYYYYYYYYYYY";
newentry.properties["userPrincipalName"]. Value= "UserID";
Tempentry.commitchanges ();
}
}
}
}
catch (Exception Raiseerr)
{
MessageBox.Show (Raiseerr.message);
}

}

Category:Active Directory

How ad uses C # for pruning, querying users and OUs

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.