更新活动目录帐户属性

时间:2015-01-15 20:22:14

标签: c# active-directory

我尝试使用C#更新某些AD帐户。我有:

void UpdateADUser(string emailaddress)
{
        try
        {
            DirectoryEntry myLdapConnection = createDirectoryEntry();
            DirectorySearcher search = new DirectorySearcher(myLdapConnection);
            search.Filter = "(cn=" + emailaddress + ")";
            search.PropertiesToLoad.Add("title");
            SearchResult result = search.FindOne();

            if (result != null)
            {
               DirectoryEntry entryToUpdate = result.GetDirectoryEntry();
               Response.Write("Current title   : " +
                                  entryToUpdate.Properties["title"][0].ToString());
            }

            else Response.Write("User not found!");
        }

        catch (Exception e)
        {
            Response.Write("Exception caught:\n\n" + e.ToString());
        }  
    }

    static DirectoryEntry createDirectoryEntry()
    {
        DirectoryEntry ldapConnection = new DirectoryEntry("leasing-vm1.**********.com");
        ldapConnection.Path = "LDAP://OU=leasing options,DC=leasing,DC=local";
        ldapConnection.AuthenticationType = AuthenticationTypes.None;
        ldapConnection.Username = "administrator";
        ldapConnection.Password = "D**********s";

        return ldapConnection;
    }  

我收到错误:

  

指定的域名不存在或无法联系。

任何帮助表示赞赏。一个潜在的问题是我的开发机器不属于相关领域。这是问题吗?

由于

1 个答案:

答案 0 :(得分:0)

如果当前计算机未加入域,则必须在LDAP路径中指定要连接的域/ DC。

e.g。 LDAP://leasing.local/OU=leasing选项,DC =租赁,DC =本地