在AD中设置manager属性而不知道管理器的URL

时间:2012-04-05 15:19:52

标签: vb.net active-directory ldap

我有一个允许我在这里编辑用户的manager属性的函数:

 Public Shared Sub SetManagerProperty(ByVal de As DirectoryEntry, ByVal pName As String, ByVal pValue As String)


        'First make sure the property value isnt "nothing"
        If Not pValue Is Nothing Then
            'Check to see if the DirectoryEntry contains this property already
            If de.Properties.Contains(pName) Then   'The DE contains this property
                'Update the properties value
                de.Properties(pName)(0) = pValue
            Else    'Property doesnt exist
                'Add the property and set it's value

                'de.Properties(pName).Add("cn=" & frmOrganization.txtManagerName.Text & ",OU=Company,OU=Users,OU=Summit,OU=North America,DC=mycompany,DC=com")


            End If
        End If

    End Sub

但是如果经理不在公司OU中怎么办?如何编辑此内容以在整个域中搜索他?

1 个答案:

答案 0 :(得分:0)

使用DirectorySearcher对象

DirectorySearcher ds = new DirectorySearcher();
ds.SearchRoot = new DirectoryEntry(String.Format("LDAP://{0}/{1}",adserver,searchroot));
ds.PropertiesToLoad.Add("distinguishedName");
ds.SearchScope = SearchScope.Subtree;
ds.Filter = String.Format("(&(objectCategory=user)(sAMAccountName={0}))", frmOrganization.txtManagerName.Text);
SearchResult sr = ds.FindOne();
if (sr == null)
{
    // Manager does not exist
    return null;
}
String managerDN = sr.Properties["distinguishedName"][0].ToString();