2011-12-21 4 views
1

Active Directory에서 정보에 액세스해야합니다. 나는 코드Active Directory Access

DirectoryEntry entry = new DirectoryEntry("LDAP://domain", "AD_id", "password"); 

DirectorySearcher search = new DirectorySearcher(entry); 
try 
{ 
    search.Filter = "(SAMAccountName=AD_id)"; 
    search.PropertiesToLoad.Add("cn"); 
    search.PropertiesToLoad.Add("sn"); 
    search.PropertiesToLoad.Add("givenName"); 
    search.PropertiesToLoad.Add("email"); 

    SearchResult result = search.FindOne(); 

    if (result != null) 
     lbl_result.Text = result.Path.ToString(); 
    else 
     lbl_result.Text = "failier"; 
} 
catch (Exception ex) 
{ 
    lbl_result.Text = ex.Message; 
} 
을 사용하고

나는 성공적으로 지정된 형식

LDAP://domain/CN=username,OU=aaa,OU=bbb,OU=ccc,DC=domain,DC=com 

에서 사용자에 대한 몇 가지 정보를 얻을 그러나 이것은 예를 들어 전자 메일 주소에 대한 내가 필요로하는 모든 정보는 위에서되지되지 않습니다 문자열. (aaa, bbb 및 ccc는 다른 정보입니다.) 내가 잘못한 일을하는 경우 도와주세요. 나는 이런 종류의 프로그래밍에 익숙하지 않다. 나는 감사 할 것이다.

답변

0

.NET 3.5 이상인 경우 System.DirectoryServices.AccountManagement (S.DS.AM) 네임 스페이스를 확인해야합니다. 여기에 대한 모든 읽기 :

// set up domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find a user 
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName"); 

if(user != null) 
{ 
    // do something here....  
    string emailAddress = user.EmailAddress; 
} 

을 : MSDN docs on System.DirectoryServices.AccountManagement

기본적으로

  • Managing Directory Security Principals in the .NET Framework 3.5
  • , 당신은 AD에서 사용자 및/또는 그룹을 쉽게 찾을 도메인 컨텍스트를 정의 할 수 있습니다 UserPrincipal 클래스에는 읽을 수있는 많은 속성이 포함되어 있습니다 (새 값도 설정 가능).

    새로운 S.DS.AM은 광고에서 사용자 및 그룹과 놀기가 정말 쉽습니다!

    물론 사용자를 검색 할 수도 있습니다.

    // create your domain context 
    PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 
    
    // define a "query-by-example" principal - here, we search for a UserPrincipal 
    // and with the SAMAccountName of "ad_id" 
    UserPrincipal qbeUser = new UserPrincipal(ctx); 
    qbeUser.SamAccountName = "ad_id"; 
    
    // 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.....   
        UserPrincipal foundUser = found as UserPrincipal; 
    
        if(foundUser != null) 
        { 
         // do something here....  
         string emailAddress = foundUser.EmailAddress; 
        } 
    } 
    
+0

내가 답장을 보내 주셔서 감사드립니다 :

당신은 당신의 검색을 할 수있는 PrincipalSearcher하고 "쿼리별로 예를 들어"주체를 사용할 수 있습니다. 시도 중입니다 ... – Lali

+0

marc_s님께, PrincipalContext 생성자가 "서버에 연결할 수 없습니다."라는 오류 메시지를 표시합니다. 그래서 나는 새로운 PrincipalContext (ContextType.Domain, "domainname")로 언급했다. 하지만 다음에 SamAccountName을 AD_id로 설정하면 "로그온 실패 : 알 수없는 사용자 이름 또는 암호가 틀립니다."오류가 표시됩니다. 새로운 UserPrincipal (ctx, "AD_id", "Pwd", true)을 설정했지만 오류는 동일합니다 ... – Lali

+0

@liaqatali : [PrincipalContext에 대한 MSDN 설명서] (http://msdn.microsoft.com/ en-us/library/system.directoryservices.accountmanagement.principalcontext.aspx) - AD에 바인딩하는 데 사용되는 자격 증명을 정의 할 수있는 여러 가지 오버로드 된 생성자가 있습니다. –