2015-02-09 41 views
0
DirectoryEntry DirEntry = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.None); 

DirectorySearcher search = new DirectorySearcher(DirEntry); 
search.Filter = String.Format("(SAMAccountName={0})", "my_login_name"); 
search.PropertiesToLoad.Add("cn"); 
SearchResult result1 = search.FindOne(); 

myDataTable Users = new myDataTable(); 
DataRow User; 

foreach (SearchResult i in search.FindAll()) 
{ 
    DirectoryEntry CurrentDirEntry; 
    User = Users.NewUserRow(); 

    CurrentDirEntry = i.GetDirectoryEntry(); 
    User.FirstName = (string)CurrentDirEntry.Properties["givenname"].Value; 
    User.LastName = (string)CurrentDirEntry.Properties["sn"].Value; 
    User.UserName = (string)CurrentDirEntry.Properties["sAMAccountName"].Value; 
    User.Email = (string)CurrentDirEntry.Properties["mail"].Value; 
    Users.AddUserRow(User); 
} 

我試圖從Active Directory讀取一些屬性但試圖返回讀取從Active Directory中的登錄名空

sAMAccountName賦

值始終返回空值,我想知道爲什麼這是如此,因爲它被匹配在搜索過濾器。它可能與訪問權限有關嗎?

我想返回名字,姓氏,電子郵件和登錄名。我收到除登錄名之外的其他屬性。

回答

0

莫非在[ 「sAMAccountName賦」]的間距:

User.UserName = (string)CurrentDirEntry.Properties["sAMAccountName "].Value; 
+0

間距實際上是一個錯字,現在糾正。 – StackTrace 2015-02-09 09:08:43

0

嘗試這一個,我以前

用它VB

Dim myDe As New DirectoryEntry("LDAP://DOMAIN.LOCAL") 
    Dim deSearcher As New DirectorySearcher(myDe) 
    Dim userDE As DirectoryEntry 
    Dim email As String = "" 

    Try 
     deSearcher.Filter = "(&(sAMAccountName=" & UserName & "))" 
     userDE = deSearcher.FindOne().GetDirectoryEntry() 
     email = userDE.Properties("mail").Value 

    Catch ex As Exception 

    End Try 

C#

DirectoryEntry myDe = new DirectoryEntry("LDAP://DOMAIN.LOCAL"); 
    DirectorySearcher deSearcher = new DirectorySearcher(myDe); 
    DirectoryEntry userDE = default(DirectoryEntry); 
    string email = ""; 

    try { 
     deSearcher.Filter = "(&(sAMAccountName=" + UserName + "))"; 
     userDE = deSearcher.FindOne().GetDirectoryEntry(); 
     email = userDE.Properties("mail").Value; 
    } catch (Exception ex) {} 
1

如果您使用的是.NET 3.5及更高版本,則應檢查System.DirectoryServices.AccountManagement(S.DS.AM)命名空間。在這裏閱讀全部內容:

基本上,你可以定義域範圍內,並可以輕鬆地查找用戶和/或組AD:

// set up domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // find a user 
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, my_login_name); 

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

的新的S.DS.AM可以很容易地與AD中的用戶和羣組玩耍!

更新:,如果你需要通過沒有被.FindByIdentity()呼叫處理字段進行搜索,那麼你需要使用PrincipalSearcher和「查詢通過例如」主要做你的搜索:

// create your domain context 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain)) 
{ 
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller" 
    UserPrincipal qbeUser = new UserPrincipal(ctx); 
    qbeUser.GivenName = "Bruce"; 
    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.....   
    } 
} 
+0

感謝您使用System.DirectoryServices.AccountManagement建議。用FirstName,LastName或Email搜索最有效的方法是什麼? – StackTrace 2015-02-09 10:42:42

+0

@ SQL.NETWarrior:更新了我的問題 - 如果您需要通過其他字段進行搜索,請使用「PrincipalSearcher」,它允許您定義一個「按示例查詢」的方法來搜索 – 2015-02-09 11:49:21

0

我不確定C#如何處理它,但我已經看到LDAP-libs以小寫形式返回屬性名稱。因此,只需撥打samaccountname而不是sAMAccountName即可。