2012-04-11 7 views
7

Active Directoryユーザーの一覧を返すメソッドを実装しました。このようなSAMAccountNameを取得したいと考えています。Domain\AdministratorActive Directoryから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\AdministratorSAMアカウント名ではありません! SAMアカウント名は、最大20文字の一意の(ドメイン全体にわたる)名前です。通常は「Windowsユーザー名」(例:Administrator)ですが、NOTにはドメイン名が含まれています。 domain\usernameの値はで、はどこにでも保存されています。


は、.NET 3.5とアップにしている場合は、System.DirectoryServices.AccountManagement(S.DS.AM)名前空間をチェックアウトする必要があります。ここではそれについてのすべてを読む:

基本的に、あなたはドメインコンテキストを定義し、簡単にADのユーザーおよび/またはグループを見つけることができます:

// 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; 
} 

新しいS.DS.AMにより、ADのユーザーやグループで簡単に遊ぶことができます。

あなたは、ユーザー(またはグループまたはコンピュータ)のグループ全体を検索したい場合は、あなたがあなたの検索を行うこと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

あなたはオブジェクトのSIDとSystem.Security.Principal.SecurityIdentifier.Translateコマンドを使用して、ユーザーを識別名としてDOMAIN \ SAMaccountフォームに変換できます。

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

「一部またはすべてのID参照は翻訳できませんでした」翻訳に誤りがあります。 – Shesha

+0

マルチドメインフォレスト?広告接続がフォレストGCに接続されていることを確認する必要があります。あなたのエンティティが確かに孤児であるかもしれません。 –