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;
dr["Name"] = string.Empty;
dr["mail"] = string.Empty;
dr["OU"] = string.Empty;
dr["LasLogo"] = string.Empty;
DirectoryEntry user = resEnt.GetDirectoryEntry();
var flag = resEnt.GetDirectoryEntry().Properties["Lastlogon"].Value;//获取最后登录时间
if (flag == null)
{
tmp = null;//这里也是Lastlogon当我们在AD里面创建一个计算机的时候什么都不设置的情况下的空值处理26
}
else
{
LargeInteger largeInt = (LargeInteger)resEnt.GetDirectoryEntry().Properties["Lastlogon"][0];
Int64 liTicks = largeInt.HighPart * 0x100000000 + largeInt.LowPart;
if (liTicks == 0)
{
tmp = null;//这里也是Lastlogon在我们创建一个计算机的时候什么都不设置的情况下的空值处理36
}
else if (DateTime.MaxValue.Ticks >= liTicks && DateTime.MinValue.Ticks <= liTicks)
{
dr["LasLogo"] = DateTime.FromFileTime(liTicks).ToString("yyyy-MM-dd HH:mm:ss");
}
}
if (user.Properties.Contains("sAMAccountName"))
{
dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
}
if (user.Properties.Contains("Name"))
{
dr["Name"] = user.Properties["Name"][0].ToString();
}
if (user.Properties.Contains("mail"))
{
dr["mail"] = user.Properties["mail"][0].ToString();
}
if (user.Parent.Name != string.Empty && user.Parent.Name.IndexOf('=') > -1)
{
dr["OU"] = user.Parent.Name.Split('=')[1];
}
dt.Rows.Add(dr);
InsertData(user.Properties["Name"][0].ToString(), dr["mail"].ToString(), dr["LasLogo"].ToString());
this.labcount.Text = "共入【" + k + "】";
this.lbLog.Items.Add(user.Properties["Name"][0].ToString() + " " + dr["mail"].ToString() + " " + dr["LasLogo"].ToString());
this.lbLog.Refresh();
this.labcount.Refresh();
}
}
catch (Exception ex)
{
this.tim190.Interval = Convert.ToInt32(this.numTime.Value.ToString()) * 60000;
}
}