2012-09-27 3 views
1

여기에 미쳐 가고 있습니다. 정말 도움이되었습니다. 단순히 DirectoryEntry 클래스를 사용하여 Active Directory에서 사용자 이름 또는 기타 항목을 가져 오려고합니다.Directoryentry에서 사용자를 가져올 수 없습니다.

userprinciple을 사용했는데 훌륭하게 작동하지만, (사용자의 관리자)에게 필요한 속성은 DirectoryEntry에서만 사용할 수 있습니다.

내 문제는 내가 온라인에서 너무 많이 보였고 거기에서 코드를 얻었지만 어떤 이유로 결코 작동하지 않으며 항상 Null을 반환합니다. 여기 예가 있습니다 :

public static DirectoryEntry GetUser(string UserName) 
{ 
    //create an instance of the DirectoryEntry 
    DirectoryEntry de = new DirectoryEntry("LDAP://" + "OU=AnotherOU,OU=xx,OU=Testvironments,DC=abc,DC=local"); 

    //create instance fo the direcory searcher 
    DirectorySearcher deSearch = new DirectorySearcher(de); 

    deSearch.SearchRoot = de; 
    //set the search filter 
    deSearch.Filter = "(&(objectCategory=user)(cn=" + UserName + "))"; 
    //deSearch.SearchScope = SearchScope.Subtree; 

    //find the first instance 
    SearchResult results = deSearch.FindOne(); 

    //if found then return, otherwise return Null 
    if (results != null) 
    { 
     //de= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure); 
     //if so then return the DirectoryEntry object 
     return results.GetDirectoryEntry(); 
    } 
    else 
    { 
     return null; 
    } 
} 

이 코드가 null을 반환하는 이유는 없습니다.

미리 감사드립니다.

답변

2

당신이 당신을 위해 무엇을 찾고있는이

//create instance for directory entry 
DirectoryEntry de = new DirectoryEntry("LDAP://" + "OU=AnotherOU,OU=xx,OU=Testvironments,DC=abc,DC=local"); 

//create instance fo the directory searcher 
DirectorySearcher deSearch = new DirectorySearcher(de);; 

//set the search filter 
deSearch.Filter = "(&(objectClass=user)(|(SAMAccountName=" + UserName+ ")(givenName=" + UserName+ ")(name=" + UserName+ ")(SN=" + UserName+ "))"; 

//find the first instance 
SearchResult results = deSearch.FindOne(); 

//if found then return, otherwise return Null 
if (results != null) 
{ 
    //The desired property you want , you can extract in this way. 
    DomainName = results .Properties["SamAccountName"][0].ToString(); 
    return domainName 
} 
else 
{ 
    return null; 
} 

희망처럼 시도 할 수 있습니다.

+0

여기에 도메인 이름은 사용자가 기기에 로그인하려는 사용자 ID입니다. 고유 ID입니다. 죄송합니다. 여기에 다른 것을 사용 했어야합니다 ... – RL89

0

cn, samAccountname, displayName 또는 userPrincipalName 속성을 원하십니까? samAccountName은 전통적인 (NT 4.0) 스타일 사용자 이름이고 displayName은 일반적으로 성을 더한 성이며 userPrincipalName은 전자 메일 주소 ([email protected])와 비슷한 형식입니다.

다른 방법으로 쿼리를 테스트하려면 ldp.exe과 같은 대화 형 LDAP 쿼리 도구를 사용하십시오. 아마도 코드에서 시도하는 것보다 훨씬 쉬울 것입니다.

관련 문제