尝试从Active Directory读取登录名,返回null

时间:2015-02-09 08:45:11

标签: c# .net active-directory ldap

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

我试图从活动目录中读取一些属性,但是

的值
  

sAMAccountName

总是返回null,我想知道为什么会这样,因为它在搜索过滤器中匹配。它可以与访问权限相关吗?

我想返回FirstName,LastName,Email和登录名。我收到了除登录名以外的其他属性。

4 个答案:

答案 0 :(得分: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和&# 34;查询通过例如"负责你的搜索:

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

答案 1 :(得分:0)

可能是[" sAMAccountName"]中的间距:

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

答案 2 :(得分: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) {}

答案 3 :(得分:0)

我不确定C#如何处理它,但我看到LDAP-libs以小写形式返回属性名称。因此,简单地呼叫samaccountname而不是sAMAccountName可能会有所帮助。