日期:2014-05-18  浏览次数:21050 次

C#如何获取AD上的信息
DirectoryEntry de = new DirectoryEntry();
  de.Path = "LDAP://CN=Users,DC=*****,DC=*****";
  de.Username = "sz-hq\\a110904";
  de.Password = "111333";

  SearchResultCollection results;
  DirectorySearcher srch = new DirectorySearcher(de);
  srch.Filter = "(&(&(objectCategory=person)(objectClass=user)))";
  results = srch.FindAll();

  foreach (SearchResult sr in results)
  {
  DirectoryEntry objDE = sr.GetDirectoryEntry();
  string userName = objDE.Name.Substring(3);
  DropDownList1.Items.Add(userName);
  }
对于DirectoryEntry.Path的值我一直很不懂(CN,DC到底填什么),希望能给我点启示..
我的域是sz-hq,,我的登录账户是a110904密码111333,我现在是想获取域中所有的成员信息

------解决方案--------------------
private const string domainName = "10.40.1.190";//102
private const string domainName102 = "10.40.1.102";
private const string adAdmin = "xingang.zhang";
private const string password = "123689Px";
private const string ouName = "Primax Users";
private const string ConnStr = "server=10.40.1.199;database=MIAD;User Id=eacsuser;Password=Eacs.123";

public Form1()
{
InitializeComponent();
}

SqlConnection conn = new SqlConnection(ConnStr);

private void butStart_Click(object sender, EventArgs e)
{
if (this.butStart.Text.IndexOf("停止") > -1)
{
this.tim190.Enabled = false;
numTime.Enabled = true;
butStart.Text = "定時抓取AD【190|102】數據";
this.butStart.Refresh();
this.tim190.Enabled = false;
}
else
{
this.pbPdm.Minimum = 0;
this.pbPdm.Maximum = 2500;
this.pbPdm.Value = 0;
numTime.Enabled = false;
butStart.Text = "停止";
this.butStart.Refresh();
this.tim190.Enabled = true;

}

}

private void GetADUserLastLogon(string strdomainName,string stradAdmin,string strpassword)
{
string sql = "";
this.lbLog.Items.Clear();
int k = 0;
DataTable dt = new DataTable();
dt.Columns.Add("sAMAccountName");//帐号
dt.Columns.Add("Name");//姓名
dt.Columns.Add("mail"); //邮箱地址
dt.Columns.Add("OU"); //用户组织
dt.Columns.Add("LasLogo");

DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + strdomainName, stradAdmin, strpassword, AuthenticationTypes.Secure);
DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);

DirectorySearcher mySearcher = new DirectorySearcher(ou);

mySearcher.Filter = ("(objectClass=user)"); //user表示用户,group表示组
mySearcher.SizeLimit = 10000;
mySearcher.PageSize = 10000;
DateTime? tmp = null;
try
{


foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
{
DataRow dr = dt.NewRow();
dr["sAMAccountName"] = string.Empty;