Using System;
Using System.Xml;
Using System.Collections;
Using System.DirectoryServices;
Using System.Windows.Forms;
Namespace LegendNet.Common.Ldap
{
<summary>
Summary description of the Legendad.
</summary>
public class Legendad
{
Public Legendad ()
{
//
TODO: Add constructor logic here
//
}
private string AdServer;
private int adport;
private string DN;
IP address of Ad Server
public string AdServer
{
Get{return AdServer;}
Set{adserver=value;}
}
Port of Ad Server
public int Adport
{
Get{return Adport;}
Set{adport=value;}
}
DN String for Ad Server
public string Dn
{
Get{return DN;}
Set{dn=value;}
}
Public Legendad (String filePath)
{
This. Checkconfig (FilePath);
}
<summary>
private void Checkconfig (string filePath)
{
Try
{
XmlDocument _xd=new XmlDocument ();
_xd. Load (FilePath);
XmlElement root=_xd. DocumentElement;
XmlNodeList _xnl=root. getElementsByTagName ("Ad_cfg");
IEnumerator ienum = _xnl. GetEnumerator ();
Ienum. MoveNext ();
Ienum= ((XmlNode) ienum. Current). Childnodes.getenumerator ();
while (Ienum. MoveNext ())
{
XmlNode title = (XmlNode) ienum. Current;
Switch (title. Name)
{
Case "Ad_server":
{
This.adserver=title. InnerText;
Break
}
Case "Ad_port":
{
This.adport=int. Parse (title. InnerText);
Break
}
Case "DN":
{
This.dn=title. InnerText;
Break
}
}
}
}
catch (Exception e)
{
throw new Exception ("Error loading ad config file, error" +e.message);
}
}
<summary>
Log in and return user information entry
</summary>
///
If you need to display detailed information about the user, use this information
Public DirectoryEntry Login (string username,string password)
{
Try
{
String Path= " ldap://"+this.adserver+": "+this.adport+"/"+THIS.DN;
directoryentry de=new DirectoryEntry (Path,username,password);
de. RefreshCache ();
return de;
}
catch (Exception e)
{
MessageBox.Show (E.message);
return null;
}
}
public bool CheckUser (string username,string password)
{
try
{
string path= "ldap://"+this.adserver+": "+this.adport+"/"+THIS.DN";
DirectoryEntry De=new DirectoryEntry (Path,username,password);
De. RefreshCache ();
return true;
}
catch (Exception e)
{
MessageBox.Show (E.message);
return false;
}
}
Querying properties for AD users
Public ArrayList SearchInfo (DirectoryEntry de)
{
ArrayList ls=new ArrayList ();
Try
{
DirectorySearcher sear=new DirectorySearcher ();
Sear. Searchroot=de;
Sear. Searchscope=searchscope.subtree;
Scope, type, account number
Sear. Filter= "(& (Objectcategory=person) (Objectclass=user) (samaccountname=" +de. Username+ ")";
Propertiestoload.add method, which is used to set the user information to display.
Sear. Propertiestoload.clear ();
SearchResultCollection Rs=sear. FindAll ();
foreach (SearchResult r in RS)
{
Resultpropertycollection rprops=r.properties;
String Prop=null;
foreach (string name in Rprops. PropertyNames)
{
foreach (object vl in Rprops[name])
{
Prop=name+ ":" +VL. ToString ();
Ls. ADD (prop);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
return LS;
}
}
}