How Active directory uses C # to add, delete, modify, and query users and organizational units

Source: Internet
Author: User
Tags filter foreach findone tostring domain domain name in domain samaccountname
Active

First, let's take a look at what active Directory is. Without my description, look at the following URLs, or in the. NET Help document, based on the Active Directory keyword, you'll understand everything.

Next, let's take a look at the permissions. You can see all the information about the domain by "the Network Neighborhood-the entire Web--directory--demain (your domain name)", and you'll know what's going on in a rough look.
What you need to know: Users under all organizational units are in the Users (container)--demain users (group)
When using code to access, if you are a domain administrator user, you can do anything, otherwise, you can only query user properties.

private void Searchuser ()
{
string domainname = "Domain";
string groupname = "Domain Users";
String Dirmemname= "";
Obtain each user name in Domain users domains
System.DirectoryServices.DirectoryEntry Group = new System.DirectoryServices.DirectoryEntry ("winnt://" + domainname + "/" + groupname + ", group");
foreach (Object member in (IEnumerable) group. Invoke ("members"))
{
Based on user generated such as: "ldap://ou= Package software class, ou= System Development Department, ou= Information Service, 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's LDAP: After the domain administrator logs on, and obtains the user's properties.
String Strfieldsvalue= "", strfields= "";
System.DirectoryServices.DirectoryEntry myds=new System.DirectoryServices.DirectoryEntry (oupath, domain administrator name, Domain Admin 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;
}
}

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

The public void AddUser (string strpath,string username,string chinesename)//strpath increase the user to which organizational unit such as "Ldap://ou=xx Company, dc= Domain,dc=com "account number, 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)//Increase organization to STRPATH organizational unit, organization name
{
Try
{
String RootDSE;
System.DirectoryServices.DirectorySearcher dsesearcher= new System.DirectoryServices.DirectorySearcher ();
Rootdse=dsesearcher.searchroot.path;
rootdse= "ldap://ou= Fashion square, 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)
{
MessageBox.Show (Raiseerr.message);
}
}

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

private void ModifyUser ()
{
Try
{
String Domainname= "Domain";
String filterstr = "(Samaccountname=karlluo)";
System.DirectoryServices.DirectorySearcher Findme = new System.DirectoryServices.DirectorySearcher (domainname);
Findme.filter = Filterstr;
System.DirectoryServices.SearchResult findres = Findme.findone ();
String Tt=findres.path;
System.DirectoryServices.DirectoryEntry myuser = Findres.getdirectoryentry ();
String Oupath=myuser.parent.path;

DirectoryEntry Myds=new DirectoryEntry (Oupath, "Domain administrator name", "Domain Admin password");

foreach (System.DirectoryServices.DirectoryEntry tempentry in myDS. Children)
{
if (tempEntry.SchemaClassName.ToString () = = "User")
{
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);
}

}



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.