我尝试将用户添加到Active Directory,到目前为止我的代码是
using (DirectoryEntry dirEntry = new DirectoryEntry(ldapPath))
if (dirEntry.SchemaEntry.Name == "container")
{
using (DirectoryEntry newUser = dirEntry.Children.Add("CN= " + username, "User"))
{
fullname = fname + " " + lname;
newUser.Properties["sAMAccountName"].Value = username;
newUser.Properties["First name"].Value = fname;
newUser.Properties["Last name"].Value = lname;
newUser.Properties["Full name"].Value = fullname;
newUser.Properties["password"].Value = password;
newUser.CommitChanges();
}
}
当我运行程序时,我收到错误
指定的目录服务属性或值不存在。
有关如何使这项工作的任何建议?是的,我是Active Directory相关新手的新手。
答案 0 :(得分:0)
Active Directory属性需要通过 LDAP名称来解决 - 而不是您在GUI中看到的内容....
所以试试这个:
System.DirectoryServices.AccountManagement
您可以找到一个很棒的Excel电子表格,其中显示了交互式GUI中使用的名称,以及它们映射到的LDAP名称,on Richard Mueller's web site here(查看"所有Active Directory属性的电子表格"以及" Active Directory用户和计算机MMC中用户属性的电子表格。")
或者,如果您使用的是.NET 3.5或更高版本,您还可以调查新的using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, NULL, ldapPath)
{
// create a user principal object
UserPrincipal user = new UserPrincipal(ctx, username, password, true);
// assign some properties to the user principal
user.GivenName = fname;
user.Surname = lname;
user.DisplayName = fullname;
// save the user to the directory
user.Save();
}
命名空间,它允许您使用形状完美的对象来处理常见任务。
您的代码看起来像这样:
ldapPath
注意:CN=Users,DC=YourCompany,DC=com
应该是容器的LDAP路径 - 没有任何前缀,例如像LDAP://
- 没有UserPrincipal
或其他前缀。
优点是:sudo su - nobody
gem install peach
对象类已经包含了很好的,强类型和更直观的属性来处理许多基本任务,比如创建一个新用户并设置它的一些属性。