我是一名新成員,雖然我遵循這個很多:) 我的代碼連接到活動目錄以獲取功能組的成員,只給我1490個單數列表中實際有1680名奇怪成員中的成員。我在Stackoverflow和Internet上搜索了很多,但我還沒有找到答案,爲什麼代碼會導致不完整的列表。可以,任何人都可以給我任何指示。謝謝:)從活動目錄中檢索數據給出了不完整的列表
這裏是連接到Active Directory檢索數據的代碼:
public static DataTable GetAdUsers(string configSection)
{
DataRow dr;
Hashtable ADGroups = (Hashtable)ConfigurationManager.GetSection(configSection);
string adGroup;
string adGroupDesc;
string sApplication;
string sLast_Login;
string sAccount_owner;
string sPath;
DataTable dt = new DataTable();
sApplication = "Application";
dt.Columns.Add(sApplication);
dt.Columns.Add("Profile", Type.GetType("System.String"));
dt.Columns.Add("Account Name", Type.GetType("System.String"));
sLast_Login = "Last Login";
dt.Columns.Add(sLast_Login);
sAccount_owner = "Account Owner";
dt.Columns.Add(sAccount_owner);
sPath = "Path";
dt.Columns.Add(sPath);
string domainName = "myDomain";
PrincipalContext pcRoot = new PrincipalContext(ContextType.Domain, domainName);
IDictionaryEnumerator adGroupEnumerator = ADGroups.GetEnumerator();
while (adGroupEnumerator.MoveNext())
{
adGroup = adGroupEnumerator.Key.ToString();
adGroupDesc = adGroupEnumerator.Value.ToString();
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcRoot, IdentityType.SamAccountName, adGroup);
System.DirectoryServices.DirectoryEntry de = (System.DirectoryServices.DirectoryEntry)grp.GetUnderlyingObject();
foreach (string sDN in de.Properties["member"])
{
System.DirectoryServices.DirectoryEntry deMember = new System.DirectoryServices.DirectoryEntry("LDAP://" + sDN.ToString());
try
{
dr = dt.NewRow();
string output1;
string subStringE1 = "DC=";
int length1 = de.Path.ToString().Length;
int length0 = de.Path.ToString().IndexOf(subStringE1);
string str1 = de.Path.ToString().Substring(length0, length1 - length0);
string subStringE2 = ",DC";
int length2 = str1.ToString().IndexOf(subStringE2);
output1 = str1.ToString().Substring(3, length2 - 3);
dr["Application"] = "Application";
dr["Profile"] = adGroupDesc;
string AccountName = deMember.Properties["samAccountName"].Value.ToString();
dr["Account Name"] = deMember.Properties["samAccountName"].Value.ToString();
dr["Last Login"] = "";
dr["Account Owner"] = deMember.Properties["givenName"].Value.ToString() + @"-" + deMember.Properties["sn"].Value.ToString();
string Path = output1 + @"\" + adGroup + @"\" + deMember.Properties["samAccountName"].Value.ToString();
Console.WriteLine(Path);
dr["Path"] = output1 + @"\" + adGroup + @"\" + deMember.Properties["samAccountName"].Value.ToString();
dt.Rows.Add(dr);
}
catch (Exception ex)
{
Console.WriteLine("Error occured for user name" + sDN + "\n" + ex.Message);
}
}
}
return dt;
}
}
所以你使用.NET 3.5'PrincipalContext'和所有 - 然後** **爲什麼你切換回舊式.NET 2.0 'DirectoryEntry'一旦你有成員的名字?根本沒有任何意義,使事情變得更加繁瑣!你已經有一個'GroupPrincipal grp' - 你爲什麼不使用'.GetMembers()'調用來獲得組的成員?會**更容易!** –
嗨馬克,你有任何指針/鏈接在同一個。我收到的大部分海蔘結果都在.NET 2.0上。另外Afifi在Msdn中的鏈接也在.NET 2.0上。 – Ashutosh
請參閱此MSDN雜誌文章:[在.NET Framework 3.5中管理目錄安全主體](http://msdn.microsoft.com/zh-cn/magazine/cc135979.aspx) –