C # connection to AD

Source: Internet
Author: User
Tags ldap ad server

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

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.