C# Active Directory:System.DirectoryServices命名空间。获得;传递的目录路径名无效";
在本教程之后,我尝试编写一篇文章,使用C# Active Directory:System.DirectoryServices命名空间。获得;传递的目录路径名无效";,c#,active-directory,ldap,C#,Active Directory,Ldap,在本教程之后,我尝试编写一篇文章,使用System.DirectoryServices命名空间将用户添加到Active Directory,但每次尝试都会遇到标题中提到的错误 正如错误所示,我查看了路径名的结构,但我仍然有疑问 我的目标是添加一个新用户并将该用户放入广告组。 从技术上讲,我们的“集团”实际上只是上级DC下的组织单位 我们的广告层次结构通常是这样格式化的… public static string CreateUserAccount() { try
System.DirectoryServices
命名空间将用户添加到Active Directory,但每次尝试都会遇到标题中提到的错误
正如错误所示,我查看了路径名的结构,但我仍然有疑问
我的目标是添加一个新用户并将该用户放入广告组。
从技术上讲,我们的“集团”实际上只是上级DC下的组织单位
我们的广告层次结构通常是这样格式化的…
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>";
}
其他指定用途(部门名称)>其他指定用途(用户)>CN(用户)
我还假设我可以在添加用户的新帐户时为用户设置某些属性,尽管我不确定这有什么限制
下面是我编写的代码。我已经阅读了一些关于代码项目的文章,但我不确定这是否只是我缺乏理解。当然,这并不像我说的那么难。我可能还不太了解广告。
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>";
}
公共静态字符串CreateUserAccount()
{
尝试
{
DirectoryEntryData newUserADdata=新的DirectoryEntryData();
string oGUID=string.Empty;
string connectionPrefix=“LDAP://”+“域”;
DirectoryEntry dirEntry=新的DirectoryEntry(connectionPrefix);
DirectoryEntry newUser=dirEntry.Children.Add
//基于组织单位和通用名称定义目录项
(“CN=“+newUserADdata.NewUserFirstName+newUserADdata.NewUserLastName+”,OU=“+newUserADdata.NewUserOrganizationDepartment+”,DC=域,DC=本地”,“用户”);
//为新条目准备数据
//初始登录信息
newUser.Properties[“samAccountName”].Value=newUserADdata.NewUserLoginUserName;//设置初始用户名
newUser.Invoke(“SetPassword”,新对象[]{newUserADdata.NewUserLoginPassword});//设置初始密码
newUser.Properties[“userPrincipalName”]。Value=newUserADdata.NewUserLoginUserName+“@domain.local”;//主体名称
newUser.Properties[“pwdLastSet”].Value=“0”;//将“Password Last Set”属性设置为0,以便在首次登录时调用密码更改
//一般的
newUser.Properties[“givenName”].Value=newUserADdata.NewUserFirstName;//First name
newUser.Properties[“sn”].Value=newUserADdata.NewUserLastName;//姓氏
newUser.Properties[“displayName”].Value=newUserADdata.NewUserDisplayName;//显示名称
newUser.Properties[“description”]。Value=newUserADdata.NewUserDescription;//description
newUser.Properties[“physicalDeliveryOfficeName”]。Value=newUserADdata.NewUserOffice;//Office
newUser.Properties[“telephoneNumber”]。Value=newUserADdata.newuserphone;//电话号码
newUser.Properties[“homeDrive”].Value=newUserADdata.NewUserHomeDriveLetter;//主驱动器号(H:)
newUser.Properties[“homeDirectory”].Value=newUserADdata.NewUserHomeDrivePath;//主驱动器路径
//电话
newUser.Properties[“homePhone”].Value=newUserADdata.NewUserTelephoneHome;//家庭电话号码
newUser.Properties[“pager”].Value=newUserADdata.NewUserTelephonePager;//寻呼机号码
newUser.Properties[“mobile”].Value=newUserADdata.NewUserTelephoneMobile;//移动电话号码
newUser.Properties[“facsimileTelephoneNumber”]。Value=newUserADdata.NewUserTelephoneFax;//传真号码
newUser.Properties[“ipPhone”].Value=newUserADdata.NewUserTelephoneIP;//IP电话号码
//地址
newUser.Properties[“streetAddress”].Value=newUserADdata.NewUserAddressStreet;//Street
newUser.Properties[“postOfficeBox”].Value=newUserADdata.NewUserAddressPObox;//P.O.Box
newUser.Properties[“l”].Value=newUserADdata.NewUserAddressCity;//城市
newUser.Properties[“st”].Value=newUserADdata.newuseraddstate;//州/省
newUser.Properties[“postalCode”].Value=newUserADdata.NewUserAddressZipCode;//邮政编码
newUser.Properties[“c”].Value=newUserADdata.NewUserAddressCountry;//国家/地区名称
//组织机构
newUser.Properties[“title”].Value=newUserADdata.NewUserOrganizationJobTitle;//作业标题
newUser.Properties[“department”]。Value=newUserADdata.NewUserOrganizationDepartment;//部门
newUser.Properties[“company”].Value=newUserADdata.NewUserOrganizationCompany;//company
newUser.Properties[“manager”].Value=newUserADdata.NewUserOrganizationManagerName;//管理者名称
newUser.CommitChanges();
oGUID=newUser.Guid.ToSt