Geeks With Blogs
Sharvan Dhaka blog
To retrive all imformation from avtive directory through ldap protocol
 
 
using objX509Cert = System.Security.Cryptography.X509Certificates;

string filter = "mail=*";
xd = lcl.LDAPInfo(filter);

public XmlDocument LDAPInfo(string filter)
{
XmlDocument xd = new XmlDocument();
string domainAndUsername = string.Empty;
string userName = string.Empty;
string passWord = string.Empty;
string Sur = "";
string Cn = "";
string Name = "";
string GName = "";
string DGname = "";
string Member = "";
string Init = "";
string Postal = "";
string Loc = "";
string C = "";
string Mobile = "";
string HomePh = "";
string Title = "";
string Co= "";
string State = "";
string eMail = "";

string Password = "";
string SAM = "";

string MemOf = "";
string UID = "";
string Desc = "";
string UserCert = "";
string UserCertName = "";
string certno = "";
string certString64="";

Boolean chkpass = false;
AuthenticationTypes at = AuthenticationTypes.Anonymous;
StringBuilder sbb = new StringBuilder();


//****Connecting to LDAP active directory
domainAndUsername = @"LDAP://YourDomain/c=CH";
userName = "username" ;
passWord = "password";
//at = AuthenticationTypes.Secure;

//Create the object necessary to read the info from the LDAP directory
DirectoryEntry entry = new DirectoryEntry(domainAndUsername,userName, passWord,at);
DirectorySearcher mySearcher = new DirectorySearcher(entry);
SearchResultCollection results;
mySearcher.Filter = filter;

try
{
results = mySearcher.FindAll();

if (results.Count > 0)
{
sbb.Append("");
foreach(SearchResult resEnt in results)
{
sbb.Append("");
ResultPropertyCollection propcoll=resEnt.Properties;
//sbb.Append("" + propcoll.PropertyNames.Count+"");
Sur = "";
Cn = "";
Name = "";
GName = "";
DGname = "";
Member = "";
Init = "";
Postal = "";
Loc = "";
C = "";
Mobile = "";
HomePh = "";
Title = "";
Co = "";
State = "";
Password = "";
SAM = "";
MemOf = "";
UID = "";
Desc = "";
UserCert = "";
UserCertName = "";
certString64="";
//string vCardn;

StringBuilder strBMails = new StringBuilder();
StringBuilder strBCert = new StringBuilder();

foreach(string key in propcoll.PropertyNames)
{
string values = "";
if (propcoll[key].Count > 0)
values = propcoll[key][0].ToString();

#region Switch
switch (key)
{
case "sn":
Sur = values.ToString();
break;
case "cn":
Cn = values.ToString();
break;
case "name":
Name = values.ToString();
break;
case "givenname":
GName = values.ToString();
break;
case "distinguishedname":
DGname = values.ToString();
break;
case "member":
Member = values.ToString();
break;
case "initials":
Init = values.ToString();
break;
case "postalcode":
Postal = values.ToString();
break;
case "l":
Loc = values.ToString();
break;
case "c":
C = values.ToString();
break;
case "mobile":
Mobile = values.ToString();
break;
case "homephone":
HomePh = values.ToString();
break;
case "title":
Title = values.ToString();
break;
case "co":
Co = values.ToString();
break;
case "st":
State = values.ToString();
break;
case "mail":
BMails.Append("");
foreach (object valuess in propcoll[key])
{
eMail = valuess.ToString();
strBMails.Append("" + valuess.ToString() + "");
}
strBMails.Append("");
break;
case "password":
strPassword = values.ToString();
break;
case "samaccountname":
strSAM = values.ToString();
break;
case "memberof":
strMemOf = values.ToString();
break;
case "uid":
strUID = values.ToString();
break;
case "description":
strDesc = values.ToString();
break;
case "usercertificate;binary":
strBCert.Append("");
foreach (object valuess in propcoll[key])
{
FileStream fs = File.Create("./tempcer1.cer");
BinaryWriter bw = new BinaryWriter(fs);
bw.Write((byte[])valuess);
bw.Close();

objX509Cert.X509Certificate myCert = objX509Cert.X509Certificate.CreateFromCertFile("./tempcer1.cer");


strBCert.Append("" + myCert.GetSerialNumberString() + "");
strBCert.Append("" + myCert.GetName() + "");
strBCert.Append("" + myCert.GetExpirationDateString() + "");

strcertno = myCert.GetSerialNumberString();
FileInfo fi = new FileInfo("./tempcer1.cer");
if (File.Exists(myCert.GetSerialNumberString() + ".cer"))
File.Delete(myCert.GetSerialNumberString() + ".cer");
fi.MoveTo(myCert.GetSerialNumberString() + ".cer");

string cerPath = System.Web.HttpContext.Current.Server.MapPath("./Certificate/");
if (File.Exists(cerPath + myCert.GetSerialNumberString() + ".cer"))
File.Delete(cerPath + myCert.GetSerialNumberString() + ".cer");
fi.CopyTo(cerPath + myCert.GetSerialNumberString() + ".cer");
chkpass = true;

}
strBCert.Append("");
break;
}
#endregion

}
sbb.Append("" + strSur + "");
sbb.Append("" + strCn + "");
sbb.Append("" + strName + "");
sbb.Append(strBMails);
sbb.Append(strBCert);
sbb.Append("" + strGName + "");
sbb.Append("" + strGName + "");
sbb.Append("" + strMember + "");
sbb.Append("" + strInit + "");
sbb.Append("" + strPostal + "");
sbb.Append("" + strLoc + "");
sbb.Append("" + strC + "");
sbb.Append("" + strMobile + "");
sbb.Append("" + strHomePh + "");
sbb.Append("");
sbb.Append("" + strCo + "");
sbb.Append("" + strState + "");
sbb.Append("" +strPassword + "");
sbb.Append("" + strSAM + "");
sbb.Append("" + strMemOf + "");
sbb.Append("" + strUID + "");
sbb.Append("" + strDesc + "");
sbb.Append("");


}
sbb.Append("");


xd.LoadXml(sbb.ToString());
return xd;
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
sbb.Append("No");
xd.LoadXml(sbb.ToString());
return xd;
}
Posted on Sunday, October 15, 2006 5:54 PM Ldap | Back to top


Comments on this post: LDAP Get Info from Active Directory

# re: LDAP Get Info from Active Directory
Requesting Gravatar...
hi ,
i have seen your code ,but still i am in confused please ,can you send the code how to retrive the information from active directory in c#.net

rohith
rohith.j@siemens.com
Left by Rohith on Jul 02, 2007 1:45 AM

# re: LDAP Get Info from Active Directory
Requesting Gravatar...
rohit,
what type of information you want to get from ldap directory
Left by sharvan on Jul 03, 2007 12:25 PM

Your comment:
 (will show your gravatar)


Copyright © Sharvan Dhaka | Powered by: GeeksWithBlogs.net