2012-04-05 15 views
0

我有一個功能,讓我來編輯在這裏用戶的管理器屬性:設定在公元經理屬性,而不知道經理的URL

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什麼?我如何編輯這個在整個域中搜索他?

回答

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(); 
+0

的問題是,它似乎並不像信息搜索結果轉換爲字符串。我收到一個未明確的錯誤。 – Pickle 2012-04-09 14:51:21

+0

您是在轉換搜索結果還是屬性?另外,如果您更熟於使用DirectoryEntries,則還可以使用SearchResult.GetDirectoryEntry()選項 – Kodra 2012-04-10 14:24:18