我正在尝试在我的域中的特定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 =域用户,它可以工作,我收到一个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中创建它,但让我们从这个开始。
感谢您的帮助! 大卫
答案 0 :(得分:2)
如果您使用的是.NET 3.5及更高版本,则应查看System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。在这里阅读所有相关内容:
基本上,您可以定义域上下文并轻松在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中的用户和群组一起玩!
答案 1 :(得分:0)
以下示例代码允许您使用System.DirectoryServices
创建用户:
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;
namespace Exemple_ADSI12_AddUser
{
class Program
{
static void Main(string[] args)
{
/* Connection to Active Directory
*/
DirectoryEntry deBase = new DirectoryEntry("LDAP://192.168.183.100:389/OU=SousMonou,OU=MonOu,DC=dom,DC=fr", "jpb", "PWD);
/* User creation
*/
DirectoryEntry auser = deBase.Children.Add("cn=a User", "user");
auser.CommitChanges();
auser.Properties["samaccountname"].Value = "AUser";
auser.Properties["givenName"].Value = "A";
auser.Properties["sn"].Value = "User";
auser.Properties["displayName"].Value = "AUser";
auser.Properties["userPrincipalName"].Value = "AUser@dom.fr";
auser.Properties["pwdLastSet"].Value = 0;
auser.Properties["userAccountControl"].Value = 544;
auser.CommitChanges();
}
}
}
您可以不使用用户/密码,但需要在要创建对象的节点上创建DirectoryEntry。您可以通过LDAP://192.168.183.100:389/OU=SousMonou,OU=MonOu,DC=dom,DC=fr
LDAP://contoso.com/OU=Domain Users,DC=contoso,DC=com