[c#] Active Directory에서 사용자 목록을 얻으려면 어떻게해야합니까?

Active Directory에서 사용자 목록을 얻으려면 어떻게해야합니까? 사용자 이름, 이름, 성을 가져 오는 방법이 있습니까? 이것이 사용 된 유사한 게시물을 보았습니다.

 PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "YOURDOMAIN");

나는 Active Directory로 아무것도하지 않았으므로 완전히 길을 잃었습니다. 어떤 도움이라도 대단히 감사하겠습니다!



답변

Active Directory를 처음 사용하는 경우 먼저 Active Directory가 데이터를 저장하는 방법을 이해해야합니다.

Active Directory는 실제로 LDAP 서버입니다. LDAP 서버에 저장된 개체는 계층 적으로 저장됩니다. 파일 시스템에 파일을 저장하는 것과 매우 유사합니다. 이것이 바로 Directory Server와 Active Directory 라는 이름을 갖게 된 이유입니다.

Active Directory의 컨테이너 및 개체는 distinguished name. 고유 이름은 다음과 같습니다 CN=SomeName,CN=SomeDirectory,DC=yourdomain,DC=com. 기존 관계형 데이터베이스와 마찬가지로 LDAP 서버에 대해 쿼리를 실행할 수 있습니다. 이를 LDAP 쿼리라고합니다.

.NET에서 LDAP 쿼리를 실행하는 방법에는 여러 가지가 있습니다. DirectorySearcher from System.DirectoryServices또는 SearchRequest 를 사용할 수 있습니다 System.DirectoryServices.Protocol.

귀하의 질문에 대해서는 사용자 주체 개체를 구체적으로 찾으려고하기 때문에 가장 직관적 인 방법은에서 PrincipalSearcher 를 사용하는 것 System.DirectoryServices.AccountManagement입니다. Google에서 다양한 예를 쉽게 찾을 수 있습니다. 다음은 귀하가 요구하는 것을 정확히 수행하는 샘플입니다.

using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com"))
{
    using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
    {
        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();
        }
    }
}
Console.ReadLine();

AD 사용자 개체에는 여러 특성이 있습니다. 특히, givenName당신을 줄 것이다 First Name그리고 sn당신에게 줄 것이다 Last Name. 사용자 이름에 대해. 사용자 로그온 이름을 의미하는 것 같습니다. AD 사용자 개체에는 두 개의 로그온 이름이 있습니다. 하나는 samAccountNameWindows 2000 이전 사용자 로그온 이름이라고도하는입니다. userPrincipalName일반적으로 Windows 2000 이후에 사용됩니다.


답변

y 활성 계정을 필터링하려면 Harvey의 코드에 다음을 추가하십시오.

 UserPrincipal userPrin = new UserPrincipal(context);
 userPrin.Enabled = true;

처음 사용 후. 그런 다음 추가

  searcher.QueryFilter = userPrin;

모두 찾기 전에. 그리고 그것은 당신에게 활성을 가져다 줄 것입니다.


답변

확실히 여기에서 @Harvey Kwok에게 크레딧이 전달되지만, 제 경우에는 실제 UserPrincipals 목록을 얻고 싶었 기 때문에이 예제를 추가하고 싶었습니다. 이 쿼리를 미리 필터링하는 것이 더 효율적일 수 있지만 소규모 환경에서는 모든 항목을 가져온 다음 나중에 목록에서 필요에 따라 필터링하는 것이 더 쉽습니다.

필요한 항목에 따라 DirectoryEntry로 캐스트 할 필요가 없지만 일부 속성은 UserPrincipal에서 사용할 수 없습니다.

using (var searcher = new PrincipalSearcher(new UserPrincipal(new PrincipalContext(ContextType.Domain, Environment.UserDomainName))))
{
    List<UserPrincipal> users = searcher.FindAll().Select(u => (UserPrincipal)u).ToList();
    foreach(var u in users)
        {
            DirectoryEntry d = (DirectoryEntry)u.GetUnderlyingObject();
            Console.WriteLine(d.Properties["GivenName"]?.Value?.ToString() + d.Properties["sn"]?.Value?.ToString());
        }
}


답변

System.DirectoryServices.dll을 포함시킨 다음 아래 코드를 사용하십시오.

DirectoryEntry directoryEntry = new DirectoryEntry("WinNT://" + Environment.MachineName);
string userNames="Users: ";

foreach (DirectoryEntry child in directoryEntry.Children)
{
    if (child.SchemaClassName == "User")
    {
        userNames += child.Name + Environment.NewLine   ;
    }

}
MessageBox.Show(userNames);


답변