2009-03-09 3 views
1

사용자 이름과 도메인을 기반으로 사용자의 전체 이름을 검색하는 기능이 있습니다. 이 함수는 가장 된 사용자 아래의 ASP.NET 스레드에서 실행됩니다. 원격 AD 지점에서 Directory searcher를 사용할 때 속성 대신 SID 번호가 표시된다고 (다른 상자에서 발생하는지 확인할 수 없음)Active Directory 검색에서 지연 바인딩을 어떻게 극복 할 수 있습니까?

public string GetUserFullName(string userName, string domainName) 
{ 
    DirectoryEntry rootEntry = new DirectoryEntry("GC://dc=company,dc=net"); 
    string filter = string.Format("(&(objectCategory=person)(objectClass=user)(!(userAccountControl:1.2.840.113556.1.4.803:=2))(userPrincipalName={0}@{1}.company.net))", userName, domainName); 
    DirectorySearcher searcher = new DirectorySearcher(rootEntry, filter, new string[] { "displayName" }); 
    rootEntry.AuthenticationType = AuthenticationTypes.Secure; 
    searcher.PageSize = 1000; 
    searcher.ServerTimeLimit = new TimeSpan(0, 10, 0); 
    searcher.ReferralChasing = ReferralChasingOption.All; 
    searcher.Asynchronous = false; 

    SearchResult result = searcher.FindOne(); 
    if (result != null) 
    { 
     return (string) result.Properties["displayName"][0]; 
    } 
    else 
    { 
     throw new Exception("Active Directory could not resolve your user name"); 
    } 

} 

답변

3

.NET Framework의 어떤 버전을 사용하고 있습니까? AD 스터프는 .NET 3.5에서 상당히 광범위하게 개편되었으며 사용자 및 그룹을위한 강력하게 형식화 된 구조를 제공합니다.

내 동료 인 Joe Kaplan과 Ethan Wilansky (MSDN)의 우수 기사 "Managing Directory Security Principals in the .NET Framework 3.5"을 확인하십시오. 정말로 훌륭한 재료.

우선 UserPrincipal이라는 클래스가 제공됩니다.이 클래스는 강력한 형식입니다 (예 : 모든 기본 속성은 개체의 속성입니다. 정말로 도움이된다.

둘째, 당신은 PrincipalSearcher를 사용하여 좋은 "예제 별 조회"방법을 얻을 - 조 및 에단의 기사에서이 샘플을 체크 아웃 :

// create a principal object representation to describe 
// what will be searched 
UserPrincipal user = new UserPrincipal(adPrincipalContext); 

// define the properties of the search (this can use wildcards) 
user.Enabled = false; 
user.Name = "user*"; 

// create a principal searcher for running a search operation 
PrincipalSearcher pS = new PrincipalSearcher(); 

// assign the query filter property for the principal object 
// you created 
// you can also pass the user principal in the 
// PrincipalSearcher constructor 
pS.QueryFilter = user; 

// run the query 
PrincipalSearchResult<Principal> results = pS.FindAll(); 

Console.WriteLine("Disabled accounts starting with a name of 'user':"); 
foreach (Principal result in results) 
{ 
    Console.WriteLine("name: {0}", result.Name); 
} 

을 전혀 기회가 있다면,에 도착하려고합니다. 광고 물건에 대한 NET 3.5! 내 경우 { "나 displayName"}에 -

마크

+0

NET 2.0입니다. 이것을 지적 주셔서 감사합니다 !!! 애드리안 –

0

나는 편리한 도우미 라이브러리로 AD를 싸서 한 항상이 방법을 사용하고 있습니다 :

/// <summary> 
    /// Returns AD information for a specified userID. 
    /// </summary> 
    /// <param name="ntID"></param> 
    /// <returns></returns> 
    public ADUser GetUser(string ntID) 
    {   
     DirectorySearcher search = new DirectorySearcher();   

     search.Filter = String.Format("(cn={0})", ntID); 

     search.PropertiesToLoad.Add("mail"); 
     search.PropertiesToLoad.Add("givenName"); 
     search.PropertiesToLoad.Add("sn"); 
     search.PropertiesToLoad.Add("displayName"); 
     search.PropertiesToLoad.Add("userPrincipalName"); 
     search.PropertiesToLoad.Add("cn"); 

     SearchResult result = search.FindOne(); 

     return new ADUser(result); 
    } 

ADUser 강한 입력 된 속성에의 SearchResult 매핑하는 사용자 정의 클래스입니다.

귀하의 구체적인 문제가 무엇인지 모르겠지만, 항상 저에게 도움이되었습니다.

편집 : 우리 코드를 비교해 보면, 당신은 속성을 미리 불러 오기 위해 검색을하지 않는다는 것을 알 것입니다. 그것은 아마도 당신의 문제 일 것입니다.

+0

안녕 FlySwat는 DirectorySearcher 생성자는 배열을로드하는 속성이 있습니다. ADUser 클래스를 사용해 보겠습니다. 감사합니다, –

+0

ADUser는 사용자를 좀 더 제정신이게 만드는 클래스입니다. – FlySwat