Three methods of Get AD user: getad

Source: Internet
Author: User
Tags ad server samaccountname

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 );
}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.