Mahesh Yadav
asked on
Ldapv3 not returning fields which are empty in AD profile
I am trying to run the following code it is working fine but there is an issue.
the issue is it is only those fields which has a value.
All those fields which are empty are not coming.
for example, if in my user in AD has a value in email field then it is coming otherwise not.
I am also giving the code, could any one help me out here.
I am using LDapv3 here
here is the code.
-------------------------- ---------- -
public static void Main(string[] args)
{
try
{
//LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier("C BMCC-A8-HP C06", 389);
LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier("1 92.168.2.1 70", 389);
LdapConnection ldapConn = new LdapConnection(ldapDir);
//You may need to try different types of Authentication depending on your setup
ldapConn.AuthType = AuthType.Basic;
//Update the next line to include the Fully Qualified LDAP name
// of the user along with that user's password
System.Net.NetworkCredenti al myCredentials =
new System.Net.NetworkCredenti al("jet\\a dministrat or", "1nT1meTec");
ldapConn.SessionOptions.Pr otocolVers ion = 3;
//This is the actual Connection establishment here
ldapConn.Bind(myCredential s);
Console.WriteLine("LdapCon nection is created successfully.");
//Search
//SearchRequest findme = new SearchRequest("dc=jet,dc=i n", "(objectClass=printQueue)" , System.DirectoryServices.P rotocols.S earchScope .Subtree," *", "+");
SearchRequest findme = new SearchRequest("CN=Users;dc =Jet,dc=in ", "(objectClass=user)",
System.DirectoryServices.P rotocols.S earchScope .Subtree, "*", "+");
//findme.DistinguishedName = "dc=jet,dc=in"; //Find all People in this ou
//findme.Filter = "(objectClass=user)"; //The type of entry we are looking for
//findme.Scope = System.DirectoryServices.P rotocols.S earchScope .Subtree; //We want all
//entries below this ou
SearchResponse results = (SearchResponse)ldapConn.S endRequest (findme); //Run the query
//and get results
SearchResultEntryCollectio n entries = results.Entries;
List<string> lstAtributes = new List<string>();
if (entries != null && entries.Count > 0) {
SearchResultEntry entry = entries[0];
IDictionaryEnumerator attributes = entry.Attributes.GetEnumer ator();
while (attributes.MoveNext()) {
lstAtributes.Add(((Directo ryAttribut e)attribut es.Value). Name);
}
}
for (int ii = 0; ii < entries.Count; ii++)//Iterate through the results
{
SearchResultEntry entry = entries[ii];
IDictionaryEnumerator attribEnum = entry.Attributes.GetEnumer ator();
Console.WriteLine("======= ========== ========== ========== ========== ========== =========" );
while (attribEnum.MoveNext())//I terate through the result attributes
{
//Attributes have one or more values so we iterate through all the values
//for each attribute
DirectoryAttribute subAttrib = (DirectoryAttribute)attrib Enum.Value ;
for (int ic = 0; ic < subAttrib.Count; ic++)
{
//Attribute Name below
Console.Write(subAttrib.Na me + ": ");
lstAtributes.Add(subAttrib .Name);
//Attribute Sub Value below
Console.WriteLine(subAttri b[ic].ToSt ring());
}
}
}
}
catch (Exception e)
{
Console.WriteLine("\r\nUne xpected exception occured:\r\n\t" + e.GetType() + ":" + e.Message);
}
finally
{
Console.ReadLine();
}
}
the issue is it is only those fields which has a value.
All those fields which are empty are not coming.
for example, if in my user in AD has a value in email field then it is coming otherwise not.
I am also giving the code, could any one help me out here.
I am using LDapv3 here
here is the code.
--------------------------
public static void Main(string[] args)
{
try
{
//LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier("C
LdapDirectoryIdentifier ldapDir = new LdapDirectoryIdentifier("1
LdapConnection ldapConn = new LdapConnection(ldapDir);
//You may need to try different types of Authentication depending on your setup
ldapConn.AuthType = AuthType.Basic;
//Update the next line to include the Fully Qualified LDAP name
// of the user along with that user's password
System.Net.NetworkCredenti
new System.Net.NetworkCredenti
ldapConn.SessionOptions.Pr
//This is the actual Connection establishment here
ldapConn.Bind(myCredential
Console.WriteLine("LdapCon
//Search
//SearchRequest findme = new SearchRequest("dc=jet,dc=i
SearchRequest findme = new SearchRequest("CN=Users;dc
System.DirectoryServices.P
//findme.DistinguishedName
//findme.Filter = "(objectClass=user)"; //The type of entry we are looking for
//findme.Scope = System.DirectoryServices.P
//entries below this ou
SearchResponse results = (SearchResponse)ldapConn.S
//and get results
SearchResultEntryCollectio
List<string> lstAtributes = new List<string>();
if (entries != null && entries.Count > 0) {
SearchResultEntry entry = entries[0];
IDictionaryEnumerator attributes = entry.Attributes.GetEnumer
while (attributes.MoveNext()) {
lstAtributes.Add(((Directo
}
}
for (int ii = 0; ii < entries.Count; ii++)//Iterate through the results
{
SearchResultEntry entry = entries[ii];
IDictionaryEnumerator attribEnum = entry.Attributes.GetEnumer
Console.WriteLine("=======
while (attribEnum.MoveNext())//I
{
//Attributes have one or more values so we iterate through all the values
//for each attribute
DirectoryAttribute subAttrib = (DirectoryAttribute)attrib
for (int ic = 0; ic < subAttrib.Count; ic++)
{
//Attribute Name below
Console.Write(subAttrib.Na
lstAtributes.Add(subAttrib
//Attribute Sub Value below
Console.WriteLine(subAttri
}
}
}
}
catch (Exception e)
{
Console.WriteLine("\r\nUne
}
finally
{
Console.ReadLine();
}
}
ASKER
this is my mistake, actually all I need to get all attribute from Ad or any other directory service which ldapv3 is able to connect no matter whether those attributes has a value in them.
My need is to get those attributes and show them in a list to the user.
My need is to get those attributes and show them in a list to the user.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is VB Script , but I would think the same applies
http://www.rlmueller.net/ADOSearchTips.htm
Give a base, filter, and list of attributes you want.
If you say find fred, you get the user attributes for fred (only populated ones )
however if you say find fred tell me samaccountname,mail,sid then you should get those attributes back if one is null.
Mark