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

Source: Internet
Author: User
Tags findone ldap administrator password samaccountname

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.
Http://developer.ccidnet.com/pub/article/c322_a28703_p2.html

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 ).
When using code for access, if you are a domain administrator user, you can perform any operation. 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 ();
}

------------------------------------------------------------------------------------ Http://www.mscto.com/

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 = "http://www.cnblogs.com/ahjxxy/admin/ldap://ou/#baiyi fashion group, dc1_domain,dc1_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 ("http://www.cnblogs.com/ahjxxy/admin/ldap://OU/=" OUName ", DC = sedep, DC = com", strPath );
}
Catch (Exception RaiseErr)
{
MessageBox. Show (RaiseErr. Message );
}
}

------------------------------------------------------------------------------ Software Development Network

Private void ModifyUser ()
{
Try
{
String DomainName = "Domain ";
String FilterStr = "(sAMAccountname = karlluo )";
System. DirectoryServices. DirectorySearcher findimethyl = new System. DirectoryServices. DirectorySearcher (DomainName );
Findimethyl. Filter = FilterStr;
System. DirectoryServices. SearchResult FindRes = findimethyl. 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 administrator 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 = "yyyyyyyyyyyyyy ";
// NewEntry. Properties ["userPrincipalName"]. Value = "userID ";
TempEntry. CommitChanges ();
}
}
}

}
Catch (Exception RaiseErr)
{
MessageBox. Show (RaiseErr. Message );
} Software Development Network

}

 

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.