2012-10-24 2 views
4

직장에있는 AD에서 우리는 메일을 사용할 수있는 보안 그룹이 있습니다. 이 정보와AD 보안 그룹의 전자 메일 주소를 결정하십시오.

 List<GroupPrincipal> result = new List<GroupPrincipal>();    
     using (PrincipalContext domain = new PrincipalContext(ContextType.Domain, userinfo[0])) 
     using (UserPrincipal user = UserPrincipal.FindByIdentity(domain, username)) 
     { 

      if (user != null) 
      { 
       PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups(); 

       int totalGroupCounter = 0; 
       StringBuilder output = new StringBuilder(); 
       List<GroupPrincipal> securityGroups = new List<GroupPrincipal>(); 
       List<GroupPrincipal> distributionGroups = new List<GroupPrincipal>(); 

       foreach (Principal group in groups) 
       { 
        totalGroupCounter++; 

        if (((GroupPrincipal)group).IsSecurityGroup.Value)       
         securityGroups.Add((GroupPrincipal)group);       
        else       
         distributionGroups.Add((GroupPrincipal)group);       
       }     
      } 
     } 

무장 그룹의 이메일 주소를 찾을 수있는 올바른 방법은 무엇입니까 : 그래서 같은 System.DirectoryServices.AccountManagement 네임 스페이스를 사용하고?

+0

혹시이 문제를 해결 했습니까? –

+0

아니, 아직. : ( – beaudetious

+0

나는 대답을 추가했다. –

답변

0

나는 활성 디렉토리 주제에 대한 전문가 인 marc_s를 생각하지만 전자 메일 주소가 연관된 보안 그룹도 갖고있었습니다. 다음은 내가 전자 메일을 가져올 수있는 방법입니다.

private void GetGroupEmail() { 
    using (var searcher = new DirectorySearcher()) { 
     searcher.Filter = "(&(objectClass=group))"; 
     searcher.SearchRoot = entry; 
     searcher.PropertiesToLoad.Add("mail"); 

     foreach (SearchResult sr in searcher.FindAll()) { 
      var email = GetSearchResultProperty(sr, "mail"); 
     } 
    } 
} 

private string GetSearchResultProperty(SearchResult sr, string propertyName) { 
    var property = sr.Properties[propertyName]; 

    if (property != null && property.Count > 0) { 
     return (string)property[0]; 
    } else { 
     return null; 
    } 
} 
10

AccountManagement 라이브러리는 액세스 할 수있는 속성을 제한합니다. 그룹의 이메일 속성을 가져 오려면 DirectoryEntry 개체로 다시 캐스팅해야합니다.

PropertyValueCollection email = ((DirectoryEntry)group.GetUnderlyingObject()).Properties["mail"]; 
if (email.Value != null) 
{ 
    // Do something with email property 
} 
관련 문제