我正在尝试在我的域中的特定 OU 中创建用户。这是我得到的
public static string ldapPath = "LDAP://OU=Domain Users,DC=contoso,DC=com";
public static string CreateUserAccount(string userName, string userPassword)
{
DirectoryEntry ldapConnection = new DirectoryEntry("contoso.com");
ldapConnection.Path = ldapPath;
DirectoryEntry user = ldapConnection.Children.Add("CN=" + userName, "user");
return user.Guid.ToString();
}
如果我删除 OU=Domain Users,它就会起作用,并且我会收到一个 Guid。但是我的 OU 中需要这些帐户。我从 AD 用户和计算机中的 OU 本身复制了 ldapPath。我知道这是正确的。
我收到的错误是
System.Runtime.InteropServices.COMException (0x80005009): The specified directory object is not bound to a remote resource
at System.DirectoryServices.DirectoryEntry.RefreshCache()
at System.DirectoryServices.DirectoryEntry.FillCache(String propertyName)
at System.DirectoryServices.DirectoryEntry.get_NativeGuid()
at System.DirectoryServices.DirectoryEntry.get_Guid()
at ADINtegrationTest.ActiveDirectory.CreateUserAccount(String userName, String userPassword) in D:\_data\ADINtegrationTest\ADINtegrationTest\ActiveDirectoryUtils.cs:line 21
at ADINtegrationTest.Form1.Form1_Load(Object sender, EventArgs e) in D:\_data\ADINtegrationTest\ADINtegrationTest\Form1.cs:line 32
我在域的成员 Win2k8 服务器上运行它,以域管理员身份登录。我最终需要在另一个 OU 下的 OU 中创建它,但让我们从这个开始。
谢谢您的帮助!
大卫
如果您使用的是 .NET 3.5 及更高版本,您应该查看System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关内容:
- 管理 .NET Framework 3.5 中的目录安全主体
- 关于 System.DirectoryServices.AccountManagement 的 MSDN 文档
基本上,您可以定义域上下文并轻松在 AD 中查找用户和/或组:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// create a user principal object
UserPrincipal user = new UserPrincipal(ctx, "User1Acct", "pass@1w0rd01", true);
// assign some properties to the user principal
user.GivenName = "User";
user.Surname = "One";
// force the user to change password at next logon
user.ExpirePasswordNow();
// save the user to the directory
user.Save();
新的 S.DS.AM 使 AD 中的用户和组的使用变得非常容易!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)