Three methods of Get AD user: getad
1. Use the AccountManagement assembly (System. DirectoryServices. AccountManagement)
AcountManagement includes:
1. UserPrincipals
2. GroupPrincipal
3. ComputerPrincipals
4. SearchPrincipals
We can use the GroupPrincipals method to obtain a group of AD users.
Private static void AccountManagementGetUsers ()
{
Var principalContext = new PrincipalContext (ContextType. domain, "192.168.1.199", "CN = Users, DC = weihu, DC = com", ContextOptions. serverBind, "administrator", "Password ");
Var principals = new GroupPrincipal (principalContext );
Foreach (var members in principals. Members)
{
Console. WriteLine (members. DisplayName );
}
}
2. directly obtain ADuser through System. DirectoryServices
In the DirectoryServices program, we can use the DirectorySearcher method to obtain the AD User.
Private static void DirectoryConnection ()
{
Var directoryEntry = new DirectoryEntry ("LDAP: // 192.168.1.199", "administrator", "Password2 ");
Var filter = "(& (objectClass = user) (objectCategory = person) (mail = *) (company = Forefront Consulting Group ))";
Var propertiesToLoad = new [] {"sAMAccountName", "givenName", "sn", "mail", "userPrincipalName "};
Var directorySearcher = new DirectorySearcher (directoryEntry, filter, propertiesToLoad );
Var users = directorySearcher. FindAll (). Cast <SearchResult> ();
Foreach (var user in users)
{
If (user. Properties. Contains ("samaccountname "))
{
Console. WriteLine (user. Properties ["samaccountname"] [0]);
}
}
}
3. Get the AD user through System. DirectoryServices. Protocols
Private static void LdapConnection ()
{
Var server = "Ffazure01.cloudapp.net ";
Var userName = "XXX ";
Var passsword = "XXX ";
Var port = 63600;
Var filter = "Ou = Users, ou = ffcg. local, dc = ffcg, dc = local ";
Var propertiesToLoad = new string [] {"sAMAccountName "};
Try
{
// AD connection
Var ldapConnection = new LdapConnection (new LdapDirectoryIdentifier (server, port ));
LdapConnection. SessionOptions. SecureSocketLayer = true;
LdapConnection. SessionOptions. ProtocolVersion = 3;
LdapConnection. SessionOptions. VerifyServerCertificate = ServerCallback;
LdapConnection. Credential = new NetworkCredential (userName, passsword );
LdapConnection. AuthType = AuthType. Negotiate;
LdapConnection. Bind ();
Console. WriteLine ("connection success ");
// GetUser
Const string ldapSearchFilter = "(objectClass = *)";
Var searchRequest = new SearchRequest (filter, ldapSearchFilter, SearchScope. Subtree, propertiesToLoad );
Var searchResponse = (SearchResponse) ldapConnection. SendRequest (searchRequest );
If (searchResponse = null) return;
Foreach (SearchResultEntry entry in searchResponse. Entries)
{
Var name = GetStringAttributeValue (entry, "sAMAccountName ");
Console. WriteLine (name );
}
}
Catch (Exception e)
{
Hrow new Exception ("Connect AD server error ");
}
}
Private static bool ServerCallback (LdapConnection connection, X509Certificate certificate)
{
Return true;
}
Private static string GetStringAttributeValue (SearchResultEntry entry, string attribute)
{
Try
{
Var attrs = entry. Attributes;
If (! Attrs. Contains (attribute) return null;
Var directoryAttribute = attrs [attribute];
Var attr = directoryAttribute. GetValues (typeof (string). First () as string ?? "";
Return attr;
}
Catch (Exception e)
{
Throw new Exception ("cocould not get attribute" + attribute + "for" + entry. DistinguishedName, e );
}
}