2016-10-28 9 views
-1

돌아 오는 중 marc_s에게 감사드립니다. 구문DL에서 LDAP의 멤버 검색 C#

내가 모든 이메일 주소를 가져올 수있는 요구 사항이 DN 여기

public void GetInfo() 
{ 
    try 
    { 
     //Object obj; 
     DirectorySearcher search; 
     DirectoryEntry entry; 
     SearchResult result; 
     String mailid = ""; 
     bool flag = false; 

     entry = new DirectoryEntry(LDAPpath);//, Domainwithuser, password); 

     search = new DirectorySearcher(entry); 
     search.Filter = "CN=DistributionList1"; 

     int i = search.Filter.Length; 

     string str = "", str1 = ""; 

     foreach (SearchResult AdObj in search.FindAll()) 
     { 
      foreach (String objName in AdObj.GetDirectoryEntry().Properties["member"]) 
      { 
       str += Convert.ToString(objName) + "<Br>"; 
       int selIndex = objName.IndexOf("CN=") + 3; 
       int selEnd = objName.IndexOf(",OU") - 3; 
       str1 += objName.Substring(selIndex, selEnd).Replace("\\", "") + "<BR>"; 

       DirectorySearcher dsSearch = new DirectorySearcher(entry); 
       dsSearch.Filter = "CN=" + objName.Substring(selIndex, selEnd).Replace("\\", ""); 

       foreach (SearchResult rs in dsSearch.FindAll()) 
       { 
        str1 += "&lt;p align='right'><font face='calibri' color='#2266aa' size=2>" + Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["displayName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["department"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["memberOf"].Value) + "&lt;/font></p>"; 
       } 
      } 
     } 

     Response.Write("&lt;BR>" + str + "&lt;Br>" + str1 + "&lt;BR>"); 
    } 
    catch (Exception ex) 
    { 
     Response.Write("--unable to fetch--<BR>" + ex.Message); 
    } 
} 

, search.findAllDirectoryCOMException 던졌습니다 무효

말 :

나는 다음 코드를 시도 배포 목록에 속한

나는 아래 링크에서 설명 된 접근 방식을 구현하려고 않았다하지만 불행히도 그것은 작동하지 않았다 https://forums.asp.net/t/1224607.aspx?Displaying+Members+in+a+Distribution+List

어떤 도움에 감사드립니다.

감사합니다.

+1

당신은 무엇을 시도 했습니까 - 어디에서 붙어 있습니까? 우리는 ** 도움이 될 것입니다 ** - 그러나 우리는 단지 당신을위한 전체 코드를 작성하지 않을 것입니다 ... –

+0

안녕하세요 marc_s, 나는 원래의 게시물을 업데이 트했습니다. –

답변

0

System.DirectoryServices.AccountManagement (S.DS.AM) 네임 스페이스를 확인해야합니다.

// set up domain context - limit to the OU you're interested in 
// use this constructor if you want just the default domain, and search in the whole domain 
//  using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null)) 
// or use this line here to define a *container* to search inside of 
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "OU=YourOU,DC=YourCompany,DC=Com")) 
{ 
    // find the group in question - this can be either a DL, or a security group - both should be found just fine 
    GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere"); 

    // if found.... 
    if (group != null) 
    { 
     // iterate over members 
     foreach (Principal p in group.GetMembers()) 
     { 
      Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName); 
      // do whatever you need to do to those members 
     } 
    } 
} 

:이

기본적으로

, 당신은 AD에서 사용자 및/또는 그룹을 쉽게 찾을 도메인 컨텍스트를 정의 할 수 있습니다 ... DirectorySearcher 오히려 어설픈 이전보다 훨씬 쉽게 당신의 인생을 만든다 새로운 S.DS.AM을 사용하면 광고에서 사용자 및 그룹과 쉽게 놀 수 있습니다!

여기에 대해 자세히 알아보기 :

을 또한 : 난 강력 별도의을 표시하는 데이터의과 를 가져오고 을 유지 명확하고 엄격하게 추천 - 돈을 HTML 표현으로 인터 믹싱 가져 오기 - 그건 단지 진흙탕 스타일 코드입니다. 권장!

하나의 방법은 필요한 데이터를 검색합니다. List<UserPrincipal> (또는 사용자가 필요로하는 데이터를 보유 할 수있는 자신의 클래스를 정의 할 수도 있습니다) 두 번째,이 메서드는 첫 번째 메서드에서이 정보를 가져 와서 반복하여 표시합니다.

+0

고마워. 이것을 시도해보고 효과가 있는지 알려줍니다. 고마워요. –

+0

InnerException = "GroupPricipal.FindByIdentity 메서드 실행 중"서버에서 참조가 제거되었습니다. "와 함께 S.DS.AM.PrincipalOperationException이 발생합니다. –

+0

using (PrincipalContext ctx = using PrincipalContext (ContextType.Domain, null, OU = 최종 사용자, OU = 계정, DC = nos, DC = , DC = , DC = com)) {GroupPrincipal group = GroupPrincipal.FindByIdentity (ctx, "

"); foreach (group.GetMembers()의 Principal p) { Console.WriteLine ("{0} : {1}", p.StructuralObjectClass, p.DisplayName); } } –