2012-04-11 8 views
7

मैंने एक ऐसी विधि लागू की जो सक्रिय निर्देशिका उपयोगकर्ताओं की एक सूची लौटाता है, मैं इस Domain\Administrator जैसे SAMAccountName को पुनर्प्राप्त करना चाहता हूं।सक्रिय निर्देशिका से SAMAccountName को पुनर्प्राप्त कैसे करें

public Collection<software_user> GetUsersFromAD(String adConnectionString) 
{ 
    var users = new Collection<software_user>(); 

    using (var directoryEntry = new DirectoryEntry(adConnectionString)) 
    { 
     var directorySearcher = new DirectorySearcher(directoryEntry); 
     directorySearcher.Filter = "(&(objectClass=user))"; 
     var propertiesToLoad = new[] 
     { 
      "SAMAccountName", 
      "displayName", 
      "givenName", 
      "sn", 
      "mail", 
      "userAccountControl", 
      "objectSid" 
     }; 
     directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad); 

     foreach (SearchResult searchEntry in directorySearcher.FindAll()) 
     { 
      var userEntry = searchEntry.GetDirectoryEntry(); 
      var ldapUser = new software_user(); 
      ldapUser.User_name = NullHandler.GetString(userEntry.Properties["displayName"].Value); 

      if (string.IsNullOrEmpty(ldapUser.User_name)) 
       continue; 
      ldapUser.User_name = NullHandler.GetString(userEntry.Properties["SAMAccountName"].Value); 
      ldapUser.email = NullHandler.GetString(userEntry.Properties["mail"].Value); 
      ldapUser.user_shortname = NullHandler.GetString(userEntry.Properties["givenName"].Value); 
      var userAccountControl = (int)userEntry.Properties["userAccountControl"].Value; 
      //ldapUser.IsActive = (userAccountControl & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE; 
      var sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value; 
      //ldapUser.SId = sid; 
      users.Add(ldapUser); 
     } 
    } 
    return users; 
} 

उत्तर

13

सबसे पहले:Domain\Administratorनहीं एक सैम खाता नाम है

इस विधि का उपयोग मैं है! एसएएम खाता नाम एक अद्वितीय (पूरे डोमेन पर) लंबाई में 20 वर्णों का नाम है - आमतौर पर यह आपका "विंडोज उपयोगकर्ता नाम" (उदा। Administrator) है - लेकिन यह नहीं डोमेन नाम शामिल है। domain\username से बना यह मान सक्रिय निर्देशिका में कहीं भी संग्रहीत नहीं है!


आप .NET 3.5 और ऊपर पर हैं, तो आप System.DirectoryServices.AccountManagement (S.DS.AM) नाम स्थान की जांच करनी चाहिए। इसके बारे में यहाँ सब पढ़ें:

मूल रूप से, आप एक डोमेन संदर्भ को परिभाषित करने और आसानी से ईस्वी में उपयोगकर्ताओं और/या समूहों पा सकते हैं:

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find a user 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

if(user != null) 
{ 
    // do something here....  
    string samAccountName = user.SamAccountName; 
} 

नया एसडीएस.एएम एडी में उपयोगकर्ताओं और समूहों के साथ खेलने के लिए वास्तव में आसान बनाता है!

आप उन (या समूह या कंप्यूटर) की एक पूरी समूह, आप एक PrincipalSearcher और एक "क्वेरी-दर-उदाहरण के लिए" का उपयोग कर सकते प्रिंसिपल के लिए खोज करने के लिए अपने खोज करना चाहते हैं:

// create your domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// define a "query-by-example" principal - here, we search for a UserPrincipal 
// and with the last name (Surname) of "Miller" 
UserPrincipal qbeUser = new UserPrincipal(ctx); 
qbeUser.Surname = "Miller"; 

// create your principal searcher passing in the QBE principal  
PrincipalSearcher srch = new PrincipalSearcher(qbeUser); 

// find all matches 
foreach(var found in srch.FindAll()) 
{ 
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....   
} 
1

आप किसी उपयोगकर्ता को ऑब्जेक्ट के एसआईडी का उपयोग करके DOMAIN \ SAMaccount फॉर्म में एक विशिष्ट नाम के रूप में अनुवाद कर सकता है, और System.Security.Principal.SecurityIdentifier.Translate कमांड।

public Collection<software_user> GetUsersFromAD(String adConnectionString) 
    { 
      var users = new Collection<software_user>(); 

      using (var directoryEntry = new DirectoryEntry(adConnectionString)) 
      { 
        var directorySearcher = new DirectorySearcher(directoryEntry); 
        directorySearcher.Filter = "(&(objectClass=user))"; 
        var propertiesToLoad = new[] 
        { 
         "SAMAccountName", 
         "displayName", 
         "givenName", 
         "sn", 
         "mail", 
         "userAccountControl", 
         "objectSid" 
        }; 
        directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad); 

        foreach (SearchResult searchEntry in directorySearcher.FindAll()) 
        { 
          var userEntry = searchEntry.GetDirectoryEntry(); 
          var ldapUser = new software_user(); 
          ldapUser.User_name = NullHandler.GetString(userEntry.Properties["displayName"].Value); 

          if (string.IsNullOrEmpty(ldapUser.User_name)) 
           continue; 
          ldapUser.User_name = NullHandler.GetString(userEntry.Properties["SAMAccountName"].Value); 
          ldapUser.email = NullHandler.GetString(userEntry.Properties["mail"].Value); 
          ldapUser.user_shortname = NullHandler.GetString(userEntry.Properties["givenName"].Value); 
          var userAccountControl = (int)userEntry.Properties["userAccountControl"].Value; 

          //ldapUser.IsActive = (userAccountControl & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE; 
          SecurityIdentifier sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value; 
    -->      NTAccount account = (NTAccount) sid.Translate(typeof(NTAccount)); 
    -->      ldapUser.User_name = account.ToString(); 

          //ldapUser.SId = sid; 
          users.Add(ldapUser); 
        } 
      } 
      return users; 
    } 
+0

मुझे "कुछ या सभी पहचान संदर्भों का अनुवाद नहीं किया जा सका।" अनुवाद में त्रुटि। – Shesha

+0

मल्टी-डोमेन वन? आपको यह सुनिश्चित करना चाहिए कि आपका विज्ञापन कनेक्शन आपके वन जीसी के लिए है। यह भी हो सकता है कि आपकी इकाई वास्तव में एक अनाथ है। –

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