Java
/**
* JAVA reads AD user information
* Aa00a00
*/
Package com.wanda.sso.client.servlet;
Import java.util.Hashtable;
Import Javax.naming.Context;
Import javax.naming.NamingEnumeration;
Import javax.naming.NamingException;
Import Javax.naming.directory.Attribute;
Import javax.naming.directory.Attributes;
Import Javax.naming.directory.SearchControls;
Import Javax.naming.directory.SearchResult;
Import Javax.naming.ldap.InitialLdapContext;
Import Javax.naming.ldap.LdapContext;
public class Adopertest {
Public Adopertest () {
}
public void Getadinfo () {
String userName = "Xueqiang"; User name
String passwd = "123456";
String host = "10.0.4.26"; Ad Server
String port = "389"; Port
String domain = "@wanda-dev.cn"; The suffix name of the mailbox
String url = new String ("ldap://" + Host + ":" + port);
String user = Username.indexof (domain) > 0? Username:username
+ domain;
Hashtable hashenv = new Hashtable ();
String adminname = "cn=oyxiaoyuanxy,cn=users,dc=hebmc,dc=com";//ad User name
String adminname = "Xueqiang"; Note The user name: Domain\User or
//[email protected]
AdminName = "Xueqiang"; Note The user name: Domain\User or[email protected]
String AdminPassword = "123456"; Password
Hashenv.put (Context.security_authentication, "simple"); LDAP Access security level
Hashenv.put (Context.security_principal, AdminName); AD User
Hashenv.put (Context.security_credentials, AdminPassword); AD Password
Hashenv.put (Context.initial_context_factory,
"Com.sun.jndi.ldap.LdapCtxFactory"); LDAP Factory class
Hashenv.put (Context.provider_url, URL);
try {
Ldapcontext CTX = new Initialldapcontext (hashenv, NULL);
Domain node
String searchbase = "DC=WANDA-DEV,DC=CN";
LDAP Search Filter Class
String searchfilter = "Objectclass=user";
Search Controller
Searchcontrols searchctls = new Searchcontrols (); Create the
Search
Controls
Creating a Search Controller
Searchctls.setsearchscope (Searchcontrols.subtree_scope); Specify
The
Search
Scope
Set the search scope
Searchctls.setsearchscope (Searchcontrols.object_scope); //
Specify the search scope setting
String returnedatts[] = {"MemberOf", "distinguishedname",
"Pwd-last-set", "User-password", "CN"};//Custom Return properties
String returnedatts[] = {"url", "whenchanged", "EmployeeID",
"Name", "userPrincipalName", "physicalDeliveryOfficeName",
"Departmentnumber", "telephonenumber", "HomePhone",
"Mobile", "department", "sAMAccountName", "whenchanged",
"Mail"}; Customizing return properties
Searchctls.setreturningattributes (Returnedatts); Set the Return property set
Search LDAP based on domain node, filter class, and search controller set to get results
Namingenumeration answer = Ctx.search (Searchbase, Searchfilter,
SEARCHCTLS);//Search for objects using the filter
The number of initialized search results is 0
int totalresults = 0;//Specify the attributes to return
int rows = 0;
while (Answer.hasmoreelements ()) {//Traversal result set
SearchResult sr = (SearchResult) answer.next ();//Get the DN that matches the search criteria
System.out.println (++rows
+ "************************************************");
System.out.println (Sr.getname ());
Attributes attrs = Sr.getattributes ();//Get the set of properties that match the condition
if (attrs! = null) {
try {
for (namingenumeration ne = attrs.getall (); NE
. Hasmore ();) {
Attribute Attr = (Attribute) ne.next ();//Get Next property
System.out.println ("Attributeid= Property name:"
+ Attr.getid (). toString ());
Reading property values
for (namingenumeration e = Attr.getall (); E
. Hasmore (); totalresults++) {
SYSTEM.OUT.PRINTLN ("attributevalues= attribute value:"
+ E.next (). toString ());
}
System.out.println ("---------------");
Reading property values
Enumeration values = Attr.getall ();
if (values = null) {//Iteration
while (Values.hasmoreelements ()) {
SYSTEM.OUT.PRINTLN ("attributevalues= attribute value:" +
Values.nextelement ());
// }
// }
System.out.println ("---------------");
}
} catch (Namingexception e) {
System.err.println ("Throw Exception:" + e);
}
}
}
System.out
. println ("************************************************");
System.out.println ("Number:" + totalresults);
Ctx.close ();
} catch (Namingexception e) {
E.printstacktrace ();
System.err.println ("Throw Exception:" + e);
}
}
public static void Main (String args[]) {
Instantiation of
Adopertest ad = new Adopertest ();
Ad. Getadinfo ();
}
}
C#
Myldappath = "ldap://your domain name";
DirectoryEntry mysearchroot = new DirectoryEntry (Myldappath);
DirectorySearcher mydirectorysearcher = new DirectorySearcher (mysearchroot);
#region All Informations
Mydirectorysearcher.filter = ("(objectclass=user)"); User represents the users, group represents the groups
foreach (SearchResult mysearchresult in Mydirectorysearcher.findall ())
{
if (Mysearchresult! = null)
{
Get the properties of the ' Mysearchresult '.
Resultpropertycollection Myresultpropcoll;
Myresultpropcoll = mysearchresult.properties;
Console.WriteLine ("The properties of the ' Mysearchresult ' are:");
foreach (String MyKey in Myresultpropcoll.propertynames)
{
string tab = "";
Console.WriteLine (MyKey + "=");
foreach (Object mycollection in Myresultpropcoll[mykey])
{
Console.WriteLine (tab + mycollection);
}
}
Mysearchroot.dispose ();
}
}
Java and C # get user information on the AD domain