2013-05-31 63 views
3

我正在嘗試通過用戶名'admin'搜索活動目錄。我知道有一個用戶在目錄中有這個用戶名,但搜索一直沒有回來。如何使用C#通過用戶名搜索Active Directory?

var attributeName = "userPrincipalName"; 
var searchString = "admin" 
var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com") 
var mySearcher = new DirectorySearcher(ent); 
mySearcher.Filter = string.Format("(&(objectClass=user)({0}={1}))", attributeName, searchString); 

var userResult = mySearcher.FindOne(); 

userResult總是以null結尾。我很想知道爲什麼,一定有我失蹤的東西。

+0

最大的可能是你不想要的UserPrincipalName但SamAccountNamr進行搜索。 –

回答

2

事實證明「的UserPrincipalName」需要全部小寫(「的UserPrincipalName」)。很高興知道,感謝您的回覆。

4

如果您使用.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, "admin"); 

if(user != null) 
{ 
    // do something here....  
} 

隨着此代碼,您將通過以下屬性搜索該用戶:

  • DistinguishedName:身份是專有名稱(DN)。
  • Guid:標識是全局唯一標識符(GUID)。
  • Name:身份是一個名字。
  • SamAccountName:身份是安全帳戶管理器(SAM)名稱。
  • Sid:身份是安全描述符定義語言(SDDL)格式中的安全標識符(SID)。
  • UserPrincipalName:身份是用戶主體名稱(UPN)。

新的S.DS.AM使它很容易與AD中的用戶和組玩耍!

2

,如果你想堅持的DirectorySearcher,嘗試通過搜索cnsamaccountname代替

var attributeName = "cn"; 
var searchString = "admin" 
var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com") 
var mySearcher = new DirectorySearcher(ent); 
mySearcher.Filter = string.Format("(&(objectcategory=user)({0}={1}))", attributeName, searchString); 

var userResult = mySearcher.FindOne(); 
0
var attributeName = "userPrincipalName"; 
var = "admin" 

您需要更換過濾器這樣的

string filter="(&(objectCategory=person)(objectClass=user)(!sAMAccountType=805306370)(attributeName =searchString))"; 



var ent = new DirectoryEntry("LDAP://"dc=corp,dc=contoso,dc=com") 
var mySearcher = new DirectorySearcher(ent); 
mySearcher.Filter = filter; 

var userResult = mySearcher.FindOne(); 
0

這應該工作

private void showUsers(string pUserName) 
    { 
     string uid = Properties.Settings.Default.uid; 
     string pwd = Properties.Settings.Default.pwd; 
     using (var context = new PrincipalContext(ContextType.Domain, "YOURDOMAIN", uid, pwd)) 
     { 
      using (UserPrincipal user = new UserPrincipal(context)) 
      { 
       user.SamAccountName = pUserName; 
       using (var searcher = new PrincipalSearcher(user)) 
       { 
        foreach (var result in searcher.FindAll()) 
        { 
         DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; 
         Console.WriteLine("First Name: " + de.Properties["givenName"].Value); 
         Console.WriteLine("Last Name : " + de.Properties["sn"].Value); 
         Console.WriteLine("SAM account name : " + de.Properties["samAccountName"].Value); 
         Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value); 
         Console.WriteLine("Mail: " + de.Properties["mail"].Value); 

         PrincipalSearchResult<Principal> groups = result.GetGroups(); 

         foreach (Principal item in groups) 
         { 
          Console.WriteLine("Groups: {0}: {1}", item.DisplayName, item.Name); 
         } 
         Console.WriteLine(); 
        } 
       } 
      } 
     } 
     Console.WriteLine("End"); 
     Console.ReadLine(); 
    } 
相關問題