【发布时间】:2014-08-24 18:38:00
【问题描述】:
按照Howto: (Almost) Everything In Active Directory via C# 教程,我正在尝试编写一篇文章以使用System.DirectoryServices 命名空间将用户添加到Active Directory,但每次尝试都会收到标题中提到的错误。
正如错误提示的那样,我查看了路径名的结构,但我仍然有疑问。
我的目标是添加一个新用户并将该用户放在一个 AD 组中。 从技术上讲,我们的“组”实际上只是父 DC 下的组织单位。
我们的 AD 层次结构通常是这样格式化的......
OU(部门名称)> OU(用户)> CN(用户)
我还假设我可以在添加新帐户时为用户设置某些属性,尽管我不确定这有什么限制。
下面是我写的代码。除了关于 Code Project 的文章之外,我已经阅读了几篇文章,但我不确定这是否只是我缺乏理解还是什么。当然,这并不像我想的那么难。我可能对 AD 还不够了解。
public static string CreateUserAccount()
{
try
{
DirectoryEntryData newUserADdata = new DirectoryEntryData();
string oGUID = string.Empty;
string connectionPrefix = "LDAP://" + "DOMAIN";
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
DirectoryEntry newUser = dirEntry.Children.Add
// Define directory entry based on Organizational Units and Common Names
("CN=" + newUserADdata.NewUserFirstName + newUserADdata.NewUserLastName + ", OU = " + newUserADdata.NewUserOrganizationDepartment + ", DC = domain, DC = local", "user");
// Prepair Data for New Entry
// Initial Login Information
newUser.Properties["samAccountName"].Value = newUserADdata.NewUserLoginUserName; // Set Initial Username
newUser.Invoke("SetPassword", new object[] { newUserADdata.NewUserLoginPassword }); // Set Initial Password
newUser.Properties["userPrincipalName"].Value = newUserADdata.NewUserLoginUserName + "@domain.local"; // Principal Name
newUser.Properties["pwdLastSet"].Value = "0"; // Set "Password Last Set" property to 0 to invoke a password change upon first login
// General
newUser.Properties["givenName"].Value = newUserADdata.NewUserFirstName; // First name
newUser.Properties["sn"].Value = newUserADdata.NewUserLastName; // Last Name
newUser.Properties["displayName"].Value = newUserADdata.NewUserDisplayName; // Display Name
newUser.Properties["description"].Value = newUserADdata.NewUserDescription; // Description
newUser.Properties["physicalDeliveryOfficeName"].Value = newUserADdata.NewUserOffice; // Office
newUser.Properties["telephoneNumber"].Value = newUserADdata.NewUserTelephone; // Telephone Number
newUser.Properties["homeDrive"].Value = newUserADdata.NewUserHomeDriveLetter; // Home Drive Letter (H:)
newUser.Properties["homeDirectory"].Value = newUserADdata.NewUserHomeDrivePath; // Home Drive Path
// Telephones
newUser.Properties["homePhone"].Value = newUserADdata.NewUserTelephoneHome; // Home Phone Number
newUser.Properties["pager"].Value = newUserADdata.NewUserTelephonePager; // Pager Number
newUser.Properties["mobile"].Value = newUserADdata.NewUserTelephoneMobile; // Mobile Phone Number
newUser.Properties["facsimileTelephoneNumber"].Value = newUserADdata.NewUserTelephoneFax; // Fax Number
newUser.Properties["ipPhone"].Value = newUserADdata.NewUserTelephoneIP; // IP Phone Number
// Address
newUser.Properties["streetAddress"].Value = newUserADdata.NewUserAddressStreet; // Street
newUser.Properties["postOfficeBox"].Value = newUserADdata.NewUserAddressPObox; // P.O. Box
newUser.Properties["l"].Value = newUserADdata.NewUserAddressCity; // City
newUser.Properties["st"].Value = newUserADdata.NewUserAddressState; // State/Province
newUser.Properties["postalCode"].Value = newUserADdata.NewUserAddressZipCode; // Zip/Postal Code
newUser.Properties["c"].Value = newUserADdata.NewUserAddressCountry; // Country/Region Name
// Organization
newUser.Properties["title"].Value = newUserADdata.NewUserOrganizationJobTitle; // Job Title
newUser.Properties["department"].Value = newUserADdata.NewUserOrganizationDepartment; // Deparment
newUser.Properties["company"].Value = newUserADdata.NewUserOrganizationCompany; // Company
newUser.Properties["manager"].Value = newUserADdata.NewUserOrganizationManagerName; // Manager Name
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
int val = (int)newUser.Properties["userAccountControl"].Value;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Account Control Flags :: syntax :: val | hex | hex | and so on... http://support.microsoft.com/kb/305144
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
newUser.Properties["userAccountControl"].Value = val | 512; // Normal User Settings
newUser.CommitChanges();
dirEntry.Close();
newUser.Close();
}
catch (System.DirectoryServices.DirectoryServicesCOMException e)
{
return "<br /><br /><div class='alert alert-danger'><b><i class='fa fa-exclamation-triangle'></i> An Error has occured:</b> <br /><br />" + e.ToString() + "</div>";
}
return "<br /><br /><div class='alert alert-success'><b>Success:<b> <br /><br />The User has been successfully added to Active Directory.</div>";
}
知道如何让它工作吗? 我真的很感激。
更新:
对于那些通过搜索广告解决方案导致这篇文章的人..
我已经接受了 marc_s 提出的解决方案。这使事情变得更加容易并加快了开发速度。
值得一提的是UserPrincipal 类属性有点限制。我找到的解决方案是使用Principal Extensions。这将允许您向未包含的类添加其他属性,例如 physicalDeliveryOfficeName 或 facsimileTelephoneNumber。
【问题讨论】:
标签: c# active-directory ldap