2015-01-07 3 views
0

존재하지 않는 경우 사용자가 존재하지 않는 도메인을 입력하면이 그러나 시험에 잘 작동내가 호출하는 경우는 DirectoryEntry는, asp.net의 C#에서 디렉토리 항목을 사용하여

ADUtils newAdClass = new ADUtils("dl-dom", "ad.test", "Password?1"); 
    List<string> domUsers = newAdClass.GetDomainUsers(); 
---------------------------------------------------------------------------------- 
------------------------------------------------------------------------------- 

public List<string> GetDomainUsers() 
{ 
    //returned list 
    List<string> domainUsers = new List<string>(); 

    //create connection 
    DirectoryEntry entry = new DirectoryEntry(_lDAPPath, _ldapUser, _ldapPassword); 
    DirectorySearcher search = new DirectorySearcher(entry); 

    //search subtree nodes 
    search.SearchScope = SearchScope.Subtree; 

    //Active Directory LDAP: All email users (alternate) 
    search.Filter = "(&(objectClass=user)(objectcategory=person))"; 

    //create results objects from search object 
    SearchResultCollection results = search.FindAll(); 

    //run through list, for each entry remove 'CN=' and add 'user' to list 
    for (int i = 0; i < results.Count; i++) 
    { 
     DirectoryEntry de = results[i].GetDirectoryEntry(); 
     string user = de.Name.Replace("CN=", ""); 
     domainUsers.Add(user); 
    } 
    return domainUsers; 
} 

. 예 : 메신저 http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.exists%28v=vs.110%29.aspx

존재 사용하기 만하여 DirectoryEntry 항목이 내가 문자열을 테스트하기 위해 필요로하는 객체 반환 있도록

ADUtils newAdClass = new ADUtils("FAKE-dl-dom", "ad.test", "Password?1"); 

이 내 코드 내부에서 오류가 발생, 나는 경로가 잘못 생각 ... 어떤 아이디어?

예를 들어
public ADUtils(string LDAPDomain, string ADUser, string ADUserPwd) 
{ 
    _lDAPPath = "LDAP://DC=" + LDAPDomain; 
    _ldapUser = ADUser; 
    _ldapPassword = ADUserPwd; 
} 
+0

을 ?? 내 Q- 인스턴트 메시지에서 문자열 경로가 틀렸지 만 어디에서 잘못 될지 확신하지 못했습니다. myADSPath = "LDAP : // onecity/CN = 사용자, DC = onecity, DC = corp, DC = fabrikam, DC = com "; 지금까지 내가 생각하는 유일한 것은 LDAP : // DC = DL-DOM, – John

답변

0

, 도메인을 가정은 "example.com"
경로하여 테스트 할 : 나는 위의 코드를 사용할 때

string entry1 = _lDAPPath + "," + _ldapUser + "," + _ldapPassword; 
//entry1 returns: LDAP://DC=dl-dom,ad.test,Password?1 

if (DirectoryEntry.Exists(entry1)) 
{ 
    DirectorySearcher search = new DirectorySearcher(entry); 

나는 예외

An invalid dn syntax has been specified. 

생성자를 얻을 LDAP://example.com이어야합니다.

DN을 제공하지 않으면 도메인 루트 개체에 자동으로 연결됩니다. 위의 예에서 실제로 얻는 객체는 LDAP://example.com/DC=example,DC=com

0

기능에 액세스하기 전에 테스트하지 마십시오. LDAP 디렉토리는 휘발성이이며, 아래에서 변경할 수 있습니다. 이것은 경쟁 조건입니다.

대신, try/catch 블록을 사용하고, 실패하면 예외를 처리 :

try 
{ 
    //create results objects from search object 
    SearchResultCollection results = search.FindAll(); 

    //run through list, for each entry remove 'CN=' and add 'user' to list 
    for (int i = 0; i < results.Count; i++) 
    { 
     DirectoryEntry de = results[i].GetDirectoryEntry(); 
     string user = de.Name.Replace("CN=", ""); 
     domainUsers.Add(user); 
    } 
} 
catch(Excpetion e) 
{ 
    //add code here to process the error 

    //after debugging, you may even decide to just swallow the exception 
    // and return an empty collection 
}