2011-01-26 4 views
3

현재 사용자의 관리자 사용자 이름을 반환하는 함수가 포함 된 LDAP 클래스를 만들고 있습니다.LDAP 관리자 속성

내가 CN = "이름"을 반환하려면 "관리자"속성을 사용할 수 있다는 것을 알고, OU는 = "그룹", DC = "회사"등

내가 특별히 관리자의 사용자 이름을 원하는

는, 아는 사람 있나요 특별히 관리자 이름 만 얻는 LDAP에 보낼 수있는 속성 문자열이 있다면? 그렇지 않다면 그렇게 할 수있는 다른 방법이 있습니까?

답변

1

나는 지금 이것에 대한 해결책을 알아 냈습니다.

기본적으로 LDAP의 manager 속성은 maanger 사용자의 distinguishedName 속성을 검색합니다.

따라서 관리자가 반환 한 distinguishedName이 포함 된 사용자를 LDAP에서 검색하면 해당 속성을 이러한 방식으로 얻을 수 있습니다.

1
한 가지 가능한 솔루션은이 같은 것을 사용하는 것입니다

- 이것은 당신이 .NET 3.5에있어 당신이 System.DirectoryServices.AccountManagement뿐만 아니라 모두 System.DirectoryServices를 참조하는 것을 필요

// return manager for a given user 
public UserPrincipal GetManager(PrincipalContext ctx, UserPrincipal user) 
{ 
    UserPrincipal result = null; 

    if (user != null) 
    { 
     // get the DirectoryEntry behind the UserPrincipal object 
     DirectoryEntry dirEntryForUser = user.GetUnderlyingObject() as DirectoryEntry; 

     if (dirEntryForUser != null) 
     { 
      // check to see if we have a manager name - if so, grab it 
      if (dirEntryForUser.Properties["manager"] != null) 
      { 
       string managerDN = dirEntryForUser.Properties["manager"][0].ToString(); 

       // find the manager UserPrincipal via the managerDN 
       result = UserPrincipal.FindByIdentity(ctx, managerDN); 
      } 
     } 
    } 

    return result; 
} 

그리고 당신은이 메서드 호출 할 수 있습니다 예 이렇게 :

// Create default domain context 
PrincipalContext ctx = new PrincipalContext(ContextType.Domain); 

// find yourself - you could also search for other users here 
UserPrincipal myself = UserPrincipal.Current; 

// get the manager for myself 
UserPrincipal myManager = GetManager(ctx, myself); 
3

위의 GetManager는 관리자가 설정되지 않은 경우를 제외하고 훌륭하게 작동합니다. 이 시나리오를 수용하기위한 약간의 변경 :

// return manager for a given user 
public UserPrincipal GetManager(PrincipalContext ctx, UserPrincipal user) { 
    if (user != null) { 
     // get the DirectoryEntry behind the UserPrincipal object 
     var dirEntryForUser = user.GetUnderlyingObject() as DirectoryEntry; 

     if (dirEntryForUser != null) { 
      // check to see if we have a manager name - if so, grab it 
      if (dirEntryForUser.Properties["manager"] != null && dirEntryForUser.Properties["manager"].Count > 0) { 
       string managerDN = dirEntryForUser.Properties["manager"][0].ToString(); 
       // find the manager UserPrincipal via the managerDN 
       return UserPrincipal.FindByIdentity(ctx, managerDN); 
      } 
     } 
    } 
    return null; 
} 
0

두 예제 모두 정상적으로 작동합니다. 그러나 개선의 여지가 있다고 생각합니다.

매개 변수가 null인지 확인하고 null을 반환하는 것이 나쁜 습관이 아니어야합니다. 반면에 hrowing 예외가 더 적절합니다.

컨텍스트를 묻는 대신 제공되는 에서 컨텍스트를 검색 할 수 있어야하므로 현재 사용자를 찾는 데 문제가 없어야합니다.

public static UserPrincipal GetManager(UserPrincipal user) 
{ 
    var userEntry = user.GetUnderlyingObject() as DirectoryEntry; 
    if (userEntry.Properties["manager"] != null 
    && userEntry.Properties["manager"].Count > 0) 
    { 
    string managerDN = userEntry.Properties["manager"][0].ToString(); 
    return UserPrincipal.FindByIdentity(user.Context,managerDN); 
    } 
    else 
    throw new UserHasNoManagerException(); 
} 

class UserHasNoManagerException : Exception 
{ }