这是创建新Active Directory用户的代码:
public string CreateUserAccount(string ldapPath, string userName,
string userPassword)
{
try
{
string oGUID = string.Empty;
string connectionPrefix = "LDAP://" + ldapPath;
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
DirectoryEntry newUser = dirEntry.Children.Add
("CN=" + userName, "user");
newUser.Properties["samAccountName"].Value = userName;
newUser.CommitChanges();
oGUID = newUser.Guid.ToString();
newUser.Invoke("SetPassword", new object[] { userPassword });
newUser.CommitChanges();
dirEntry.Close();
newUser.Close();
}
catch (System.DirectoryServices.DirectoryServicesCOMException E)
{
//DoSomethingwith --> E.Message.ToString();
}
return oGUID;
}
但用户在哪里创建?我在AD中有很多子文件夹,我想将新用户放在一个特定的文件夹中。
如何在创建新用户时提交路径?
路径示例:domain / groupname / groupsubfolder / externalusers / user
答案 0 :(得分:1)
但是用户在哪里创建了?
string connectionPrefix = "LDAP://" + ldapPath;
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
DirectoryEntry newUser = dirEntry.Children.Add
("CN=" + userName, "user");
根据您共享的代码,将在值ldapPath
寻址的容器中创建用户。无论在ldapPath中传递什么值,都将在该父容器内创建用户。
我想将新用户放在特定文件夹中。路径示例: 域/组名/ groupsubfolder / externalusers /用户
由于您在方法参数的帮助下绑定了DirectoryEntry节点,因此需要传递以下值来代替ldapPath。
ldapPath = "OU=user,OU=externalusers,OU=groupsubfolder,OU=groupname,DC=domain,DC=name";
// assuming that user, externalusers, groupsubfolder and groupname are all OUs
// then place the call to method CreateUserAccount
// and pass this ldapPath as the string value.
CreateUserAccount(ldapPath, userName, userPassword);
如何在创建新用户时提交路径?
string connectionPrefix = "LDAP://" + ldapPath;
// if you change the ldapPath here to the OU where you want the user to be created,
// you'll get the desired result.
DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix);
DirectoryEntry newUser = dirEntry.Children.Add
("CN=" + userName, "user");
如果您想更改用户创建的路径,请将ldapPath的值更改为您希望在上面的注释中突出显示用户的OU / Container。
执行第一次提交后立即创建用户。检查负责用户创建的代码中的这一行:
newUser.Properties["samAccountName"].Value = userName;
newUser.CommitChanges();