2012-02-27 6 views
0

J'ai écrit le code pour l'authentification de l'utilisateur Active Directory LDAP Il authentifie tous les comptes utilisateurs dans AD, mais je ne veux que l'authentification du compte Administrateur pas d'autre compte utilisateur (voir code ci-dessous). nom de connexion DNS (voir image ci-jointe)Authentification LDAP uniquement pour le compte Admin

 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

Répondre

2

Essayez ce code.

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

Merci .it fonctionne très cool – soundy