2012-04-11 32 views
7

Tôi đã triển khai phương thức trả về danh sách người dùng Active Directory, tôi muốn truy xuất SAMAccountName như thế này Domain\Administrator.Cách truy xuất SAMAccountName từ Active Directory

Đây là phương pháp tôi sử dụng:

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

Trả lời

13

Trước hết:Domain\AdministratorKHÔNG Tên tài khoản SAM! Tên tài khoản SAM là tên duy nhất (trên toàn bộ tên miền) có độ dài tối đa 20 ký tự - thường là "tên người dùng Windows" của bạn (ví dụ: Administrator) - nhưng nó NOT bao gồm tên miền. Giá trị đó được tạo thành từ domain\usernameNOT được lưu trữ trong Active Directory ở bất kỳ đâu!


Nếu bạn đang sử dụng .NET 3.5 trở lên, bạn nên kiểm tra không gian tên System.DirectoryServices.AccountManagement (S.DS.AM). Đọc tất cả về nó ở đây:

Về cơ bản, bạn có thể xác định một bối cảnh miền và dễ dàng tìm người sử dụng và/hoặc nhóm trong 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; 
} 

Các new S.DS.AM làm cho nó thực sự dễ dàng để chơi xung quanh với người dùng và các nhóm trong AD!

Nếu bạn muốn tìm kiếm một nhóm toàn của người sử dụng (hoặc nhóm hoặc máy tính), bạn có thể sử dụng một PrincipalSearcher và một "truy vấn theo ví dụ" chủ yếu để làm tìm kiếm của bạn:

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

Bạn có thể dịch người dùng dưới dạng tên Phân biệt thành biểu mẫu DOMAIN \ SAMaccount bằng SID của đối tượng và lệnh 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

Tôi nhận được "Một số hoặc tất cả các tham chiếu danh tính không thể dịch được". lỗi trong dịch. – Shesha

+0

Rừng nhiều miền? Bạn nên chắc chắn rằng kết nối quảng cáo của bạn là GC rừng của bạn. Cũng có thể là thực thể của bạn thực sự là một đứa trẻ mồ côi. –

Các vấn đề liên quan