常用AD操作二则
1、获得用户的所有属性
string strPath = "LDAP://CN=王林,OU=HXZ,OU=gzzf,DC=iw,DC=local";
DirectoryEntry myEntry = new DirectoryEntry(strPath);
DirectorySearcher mySearcher = new DirectorySearcher(myEntry);
SearchResult myResult = mySearcher.FindOne();
if (myResult != null)
{
ResultPropertyCollection myResultProColl;
myResultProColl = myResult.Properties;
foreach (string myKey in myResultProColl.PropertyNames)
{
foreach (Object myCollection in myResultProColl[myKey])
{
this.textBox1.Text += "PropertyName:" + myKey + " = " + myCollection + "\r\n";
}
}
}
string ADpath = "LDAP://DC=iw,DC=local";
string ADUser = "iw\\admin";
string ADPassword = "password";
string groupName = "workgroup"; //组名
string userPath = "CN=王林,OU=HXZ,OU=gzzf,DC=iw,DC=local";
DirectoryEntry entry = new DirectoryEntry(ADpath,ADUser,ADPassword,AuthenticationTypes.Secure);
DirectorySearcher deSearch = new DirectorySearcher(entry);
deSearch.Filter = "(&(objectClass=group)(cn=" + groupName +"))";
deSearch.SearchScope = SearchScope.Subtree;
try
{
SearchResult result = deSearch.FindOne();
DirectoryEntry deGroup = new DirectoryEntry(result.Path);
deGroup.Properties["member"].Add(userPath);
//或者用下面方法获得用户。
//deGroup.Properties["member"].Add(deUser.Properties["distinguishedName"].Value);
deGroup.CommitChanges();
}
catch (Exception ex)
{
throw ex;
}

浙公网安备 33010602011771号