2015-02-09 24 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即可。