2012-02-27 2 views
0

Active Directory LDAP 사용자 인증을위한 코드 작성 AD에서 모든 사용자 계정을 인증하지만 다른 사용자 계정 (아래 코드 참조)이 아닌 관리자 계정 인증 만 원합니다. (첨부 이미지 참조) DNS를 연결의 이름을LDAP 계정 만 관리자 계정으로 인증

 try 
     { 
      DirectoryEntry entry = new DirectoryEntry(Domain, UserName, Password); 
      object nativeObject = entry.NativeObject; 
      Program.fileWrite.WriteLine(DateTime.Now + "\t Login with credentials " + UserName + " and " + Password); 
      return true; 
     } 
     catch (DirectoryServicesCOMException e) 
     { 
      Program.fileWrite.WriteLine(DateTime.Now + "\t " + e.Message); 
      return false; 
     } 

login page

답변

2

이 코드 시도해보십시오.

public static bool ValidateCredential(string domain, string userName, string password) 
    { 
     using (var context = new PrincipalContext(ContextType.Domain, domain)) 
     { 
      using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userName)) 
      { 
       if (user == null) return false; 

       using (var group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "Domain Admins")) 
       { 
        if (group == null) return false; 

        foreach (var member in group.GetMembers()) 
        { 
         if (member.Sid.Equals(user.Sid)) 
         { 
          return context.ValidateCredentials(userName, password); 
         } 
        } 
       } 
      } 
     } 

     return false; 
    } 
+0

덕분에 아주 멋진 작품 .IT를 – soundy