我用Add方法加入一个用户,居然没成功,AD内找不到。源码如下:
string strType = "";
if(radio4.Checked)
strType = "user";
else if(radio5.Checked)
strType = "group";
else if(radio6.Checked)
strType = "organizationalUnit"; string strPath = ""; if(radio1.Checked)
strPath = "LDAP://dcmail/OU=" + txt1.Text +",DC=Ondocs,DC=COM";
else if(radio2.Checked)
strPath = "LDAP://dcmail/CN=" + txt1.Text + ",DC=Ondocs,DC=COM";
else
strPath = "LDAP://dcmail/DC=Ondocs,DC=COM"; DirectoryEntry de = new DirectoryEntry(strPath);
de.Username = "administrator";
de.Password = "On2003Docs";
de.Children.Add(txtName.Text,strType);
de.CommitChanges();
请高手指点。
string strType = "";
if(radio4.Checked)
strType = "user";
else if(radio5.Checked)
strType = "group";
else if(radio6.Checked)
strType = "organizationalUnit"; string strPath = ""; if(radio1.Checked)
strPath = "LDAP://dcmail/OU=" + txt1.Text +",DC=Ondocs,DC=COM";
else if(radio2.Checked)
strPath = "LDAP://dcmail/CN=" + txt1.Text + ",DC=Ondocs,DC=COM";
else
strPath = "LDAP://dcmail/DC=Ondocs,DC=COM"; DirectoryEntry de = new DirectoryEntry(strPath);
de.Username = "administrator";
de.Password = "On2003Docs";
de.Children.Add(txtName.Text,strType);
de.CommitChanges();
请高手指点。
{
string strDomain="";
String RootDSE;
System.DirectoryServices.DirectorySearcher DSESearcher
= new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
MessageBox.Show(RootDSE);
RootDSE=RootDSE.Insert(7,"CN=Users,");
MessageBox.Show(RootDSE);
MessageBox.Show(strDomain);
DirectoryEntry myDE = new DirectoryEntry(RootDSE);
DirectoryEntries myEntries = myDE.Children;
// Create a new entry 'Sample' in the container.
DirectoryEntry myDirectoryEntry =
myEntries.Add("CN=aaa", "user");
MessageBox.Show(myDirectoryEntry.SchemaClassName.ToString());
myDirectoryEntry.Properties["userPrincipalName"].Value="aaa";
myDirectoryEntry.Properties["name"].Value="aaa";
myDirectoryEntry.Properties["samAccountName"].Value="aaa";
myDirectoryEntry.Properties["userAccountControl"].Value =66048; //590336;
myDirectoryEntry.CommitChanges();
}
{ string DomainName="szaic";
string FilterStr = "(sAMAccountname=aaa)";
DirectorySearcher FindMe = new DirectorySearcher(DomainName);
FindMe.Filter = FilterStr;
SearchResult FindRes = FindMe.FindOne();
DirectoryEntry MyUser = FindRes.GetDirectoryEntry();
string OUPath=MyUser.Parent.Path; DirectoryEntry myds=new DirectoryEntry(OUPath,"域管理员名","域管理员密码");
foreach(DirectoryEntry tempEntry in myds.Children)
{
if(tempEntry.SchemaClassName.ToString() == "user")
{
if(tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()=="aaa")
{
tempEntry.UsePropertyCache=true;
//tempEntry.Properties["postalCode"].Value = "111111";
tempEntry.Properties["displayName"].Value = "bbbbb";
//myds.Properties[]
myds.CommitChanges();
}
}
}
}
{
string DomainName="szaic";
string FilterStr = "(sAMAccountname=aaa)";
DirectorySearcher FindMe = new DirectorySearcher(DomainName);
FindMe.Filter = FilterStr;
SearchResult FindRes = FindMe.FindOne();
DirectoryEntry MyUser = FindRes.GetDirectoryEntry();
string OUPath=MyUser.Parent.Path; DirectoryEntry myds=new DirectoryEntry(OUPath,"域管理员名","域管理员密码");
foreach(DirectoryEntry tempEntry in myds.Children)
{
if(tempEntry.SchemaClassName.ToString() == "user")
{
if(tempEntry.Properties["sAMAccountName"].Value.ToString().ToLower()=="aaa")
{
myds.Children.Remove(tempEntry);
myds.CommitChanges();
}
}
}
}
RootDSE=RootDSE.Insert(7,"CN=organizationalUnit,");DirectoryEntry myDirectoryEntry =
myEntries.Add("CN=aaa", "organizationalUnit");
并将下面的属性设置改为OU相应的属性名称。
System.DirectoryServices.DirectorySearcher DSESearcher
= new System.DirectoryServices.DirectorySearcher();
RootDSE=DSESearcher.SearchRoot.Path;
DirectoryEntry myDE = new DirectoryEntry(RootDSE);
DirectoryEntries myEntries = myDE.Children;
DirectoryEntry myDirectoryEntry =
myEntries.Add("OU=aaa","organizationalUnit"); myDirectoryEntry.Properties["name"].Value="aaa";
myDirectoryEntry.Properties["instanceType"].Value=4;
myDirectoryEntry.Properties["distinguishedName"].Value="OU=aaa,这里换成你的DC(是不是这个:DC=Ondocs,DC=COM)";
myDirectoryEntry.Properties["objectCategory"].Value="CN=Organizational-Unit,CN=Schema,CN=Configuration,这里换成你的DC";
myDirectoryEntry.Properties["ou"].Value="aaa";
myDirectoryEntry.Properties["postalCode"].Value="777"; myDirectoryEntry.CommitChanges();
以上这句执行错误,提示不正确的dn语法。另外请问用户组怎么添加?
把myDirectoryEntry.Properties[""]这些话都去掉,也可以建上,这些只是建的时候设置的一些属性,都可以查出来。需要的时候再去设置就行了。
我没有做过添加用户。但是应该和添加用户和OU是差不多的,只是classObject不一样。