(System.DirectoryServices.DirectoryServicesCOMException E) { // DoSomethingwith - > E.Message.ToString();
} 返回oGUID;}
执行此方法时发生以下错误:
“服务器不是 操作 </跨度> “
说我们安装了域名的活动目录 TestDomain.com 并且你有一个OU(组织单位) USERS 并且你有一个用户在其中调用 TestUser
TestDomain.com
USERS
TestUser
所以我们可以说以下内容
的 ldapDomain 强> :完全限定的域名为TestDomain.com或dc = contoso,dc = com 的 objectPath 强> :对象的完全限定路径:CN = TestUser,OU = USERS,DC = TestDomain,DC = com 的 用户DN 强> :用户的distinguishedName:CN = TestUser,OU = USERS,DC = TestDomain,DC = com
在创建用户时,您应该通过确定其路径来确定要创建的位置(ldap路径)
在我们的示例中,我们可以将其视为如下:
string ldapPath = "LDAP://OU=USERS, DC=TestDomain, DC=com"
有关更多信息,请查看以下链接: http://www.selfadsi.org/ldap-path.htm http://www.informit.com/articles/article.aspx?p=101405&seqNum=7 http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.path.aspx
使用System.DirectoryServices
To use this namespace you need to add reference System.DirectoryServices.dll DirectoryEntry ouEntry = new DirectoryEntry("LDAP://OU=TestOU,DC=TestDomain,DC=local"); for (int i = 3; i < 6; i++) { try { DirectoryEntry childEntry = ouEntry.Children.Add("CN=TestUser" + i, "user"); childEntry.CommitChanges(); ouEntry.CommitChanges(); childEntry.Invoke("SetPassword", new object[] { "password" }); childEntry.CommitChanges(); } catch (Exception ex) { } }
使用System.DirectoryServices.AccountManagement
To use this namespace you need to add reference System.DirectoryServices.AccountManagement.dll PrincipalContext ouContex = new PrincipalContext(ContextType.Domain, "TestDomain.local", "OU=TestOU,DC=TestDomain,DC=local"); for (int i = 0; i < 3; i++) { try { UserPrincipal up = new UserPrincipal(ouContex); up.SamAccountName = "TestUser" + i; up.SetPassword("password"); up.Enabled = true; up.ExpirePasswordNow(); up.Save(); } catch (Exception ex) { } }