2012-05-05 9 views
12

मैं main.com, sub.main.com, sub2.main.com और आदिSystem.DirectoryServices.AccountManagement का उपयोग कर एकाधिक डोमेन में कैसे खोजें?

की तरह तीन या अधिक डोमेन है मैं एक कोड है:

using (PrincipalContext ctx = 
    new PrincipalContext(ContextType.Domain, "ADServer", 
    "dc=main,dc=com", ContextOptions.Negotiate)) 
{ 
    UserPrincipal u = new UserPrincipal(ctx); 
    u.UserPrincipalName = "*" + mask + "*"; 

    using (PrincipalSearcher ps = new PrincipalSearcher(u)) 
    { 
     PrincipalSearchResult<Principal> results = ps.FindAll(); 
     List<ADUser> lst = new List<ADUser>(); 

     foreach (var item in results.Cast<UserPrincipal>().Take(15)) 
     { 
      byte[] sid = new byte[item.Sid.BinaryLength]; 
      item.Sid.GetBinaryForm(sid, 0); 

      ADUser us = new ADUser() 
      { 
       Sid = sid, 
       Account = item.SamAccountName, 
       FullName = item.DisplayName 
      }; 

      lst.Add(us); 
     } 

    } 

    return lst; 
} 

लेकिन यह केवल एक ही डोमेन के भीतर खोज: main.com

मैं एक ही समय में सभी डोमेन में रिकॉर्ड कैसे खोज सकता हूं?

+1

मुझे नहीं लगता कि से

new PrincipalContext(ContextType.Domain, "xyz.mycorp.com:3268", "DC=mycorp,DC=com"); 

आप एक ही समय में एकाधिक डोमेन में खोज सकते हैं। आपको अपनी खोजों को "क्रमबद्ध" करने की आवश्यकता है। –

+0

क्या आपका मतलब है कि मुझे डोमेन जानना है और उन्हें चक्र से खोजना है? –

उत्तर

9

यहाँ जड़ में से एक से अपने सभी डोमेन को खोजने के लिए एक तरीका है:

/* Retreiving RootDSE 
*/ 
string ldapBase = "LDAP://DC_DNS_NAME:389/"; 
string sFromWhere = ldapBase + "rootDSE"; 
DirectoryEntry root = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD"); 
string configurationNamingContext = root.Properties["configurationNamingContext"][0].ToString(); 

/* Retreiving the root of all the domains 
*/ 
sFromWhere = ldapBase + configurationNamingContext; 
DirectoryEntry deBase = new DirectoryEntry(sFromWhere, "AdminLogin", "PWD"); 

DirectorySearcher dsLookForDomain = new DirectorySearcher(deBase); 
dsLookForDomain.Filter = "(&(objectClass=crossRef)(nETBIOSName=*))"; 
dsLookForDomain.SearchScope = SearchScope.Subtree; 
dsLookForDomain.PropertiesToLoad.Add("nCName"); 
dsLookForDomain.PropertiesToLoad.Add("dnsRoot"); 

SearchResultCollection srcDomains = dsLookForDomain.FindAll(); 

foreach (SearchResult aSRDomain in srcDomains) 
{ 
} 

फिर foreach डोमेन, आप क्या जरूरत के लिए देख सकते हैं।

+0

मेरे मामले में कोई '389' बंदरगाह नहीं। –

+1

आपको अपने डोमेन पर जीसी मिलना चाहिए। – JPBlanc

+0

धन्यवाद, 'नई निर्देशिका एंटर्री ("एलडीएपी: // सर्वर आईपी/डीसी = मेरा डोमेन, डीसी = कॉम", ...)' ठीक काम किया। –

20

आपको एलडीएपी के बजाय जीसी का उपयोग करना चाहिए। यह पूरी डोमेन वन के साथ खोज

var path="GC://DC=main,DC=com"; 
try { 
    using (var root = new DirectoryEntry(path, username, password)) { 
    var searchFilter=string.Format("(&(anr={0})(objectCategory=user)(objectClass=user))", mask); 
    using (var searcher = new DirectorySearcher(root, searchFilter, new[] { "objectSid", "userPrincipalName" })) { 
    var results = searcher.FindAll(); 
    foreach(SearchResult item in results){ 
     //What ever you do 
    } 
} catch (DirectoryServicesCOMException) { 
    // username or password are wrong 
} 
+1

हे, तुम कहाँ गए हो? :-) धन्यवाद –

+0

धन्यवाद यह एक आकर्षण की तरह काम करता है और स्वीकृत उत्तर से सरल है। – Brandon

+0

ऊपर दृश्यता के लिए मतदान किया। बहुत सारे संसाधनों के माध्यम से खोज के बाद सबसे अच्छा जवाब – senthil

संबंधित मुद्दे