Active Directory 将用户移动到不同的 OU

Active Directory move a user to a different OU

我正在开发一个程序,可以为离开我们网络的用户自动执行分离过程。它执行的任务之一是将用户帐户从它所在的 OU 移动到 Former Employees OU。尽管我在使用 DirectoryServices 进行其他处理时没有遇到任何问题,但我在执行此步骤时遇到了问题。到目前为止,这是我的代码(注意:我知道我需要停止捕获和吃掉所有异常。这将在发布前得到解决和更正。关于我应该捕获哪些异常以及我不应该捕获哪些异常的任何建议也将不胜感激):

private const string AD_DOMAIN_NAME = "domain.com";
private const string AD_NEW_PASSWORD = "TestPassword123";
private const string AD_FORMER_EMPLOYEES_OU = "LDAP://OU=Former Employees,DC=domain,DC=com";

static DirectoryEntry CreateDirectoryEntry(string connectionPath, 
        string adUserName, string adPassword)
{
    DirectoryEntry ldapConnection = null;

    try
    {
        ldapConnection = new DirectoryEntry(AD_DOMAIN_NAME, adUserName, adPassword);
        ldapConnection.Path = connectionPath;
        ldapConnection.AuthenticationType = AuthenticationTypes.Secure;                
    }

    catch (Exception ex)
    {
        MessageBox.Show("Exception Caught in createDirectoryEntry():\n\n" + ex.ToString());
    }

    return ldapConnection;
}

private void btnProcessSeparation_Click(object sender, EventArgs e)
{
    if (cboOffice.SelectedItem != null && lstUsers.SelectedItem != null)
    {
        string userOU = cboOffice.SelectedItem.ToString();
        string userName = lstUsers.SelectedItem.ToString();
        string userDn = "LDAP://OU=" + userOU + ",OU=Employees,DC=domain,DC=com";

        using (DirectoryEntry ldapConnection = CreateDirectoryEntry(userDn))
        {
            using (DirectorySearcher searcher = CreateDirectorySearcher(ldapConnection,
                SearchScope.OneLevel, "(samaccountname=" + userName + ")", "samaccountname"))
            {
                SearchResult result = searcher.FindOne();

                if (result != null)
                {
                    using (DirectoryEntry userEntry = result.GetDirectoryEntry())
                    {
                        if (userEntry != null)
                        {
                            using (DirectoryEntry formerEmployees = CreateDirectoryEntry(
                                AD_FORMER_EMPLOYEES_OU))
                            {
                                userEntry.MoveTo(formerEmployees); // This line throws an DirectoryServicesCOMException.
                            }

                            userEntry.CommitChanges();
                            userEntry.Close();
                            MessageBox.Show("Separation for {0} has completed successfully.", userName);
                        }
                    }
                }
            }
        }
    }

    else
    {
        MessageBox.Show("Error, you did not select an OU or a user. Please try again.");
    }
}

上面的代码在 userEntry.MoveTo(formerEmployees); 行之前工作正常。该行抛出一个 DirectoryServicesCOMException 和附加信息 An invalid dn syntax has been specified. 这很奇怪,因为我使用的格式与其他 DirectoryEntry 的格式相同,但效果很好。我添加了一个断点并确认 formerEmployees 设置为:LDAP://OU=Former Employees,DC=domain,DC=com。我直接从 Active Directory 中 OU 的 distinguishedName 属性复制了 LDAP:// 之后的所有内容,以确保它是正确的。

OU 名称中的 space 是否导致问题?我让它工作一次就好了,然后继续执行其他任务,一定是改变了一些破坏它的东西。我一直在看太多我认为的代码并且似乎无法理解为什么它认为我正在发送无效的 dn。

感谢所有帮助!

@David 向我指出正确的方向以确保我对 OU 具有正确的权限后,我发现了问题。我添加了一个重载的 CreateDirectoryEntry 方法,它使用用户名和密码(这是我在上面的代码中输入的内容)。但是,如果您在上面的代码中注意到,我调用的方法只采用连接路径。

感谢@David 的帮助!

希望对您有所帮助:

 DirectoryEntry eLocation = Conexion.Conectar(Localitation);
 DirectoryEntry nLocation =Conexion.Conectar(NewLocalitation);
                        string newName = eLocation.Name;
                        eLocation.MoveTo(nLocation, newName);
                        nLocation.Close();
                        eLocation.Close();