感谢marc_s回来。
我尝试了以下代码:
public void GetInfo()
{
try
{
//Object obj;
DirectorySearcher search;
DirectoryEntry entry;
SearchResult result;
String mailid = "";
bool flag = false;
entry = new DirectoryEntry(LDAPpath);//, Domainwithuser, password);
search = new DirectorySearcher(entry);
search.Filter = "CN=DistributionList1";
int i = search.Filter.Length;
string str = "", str1 = "";
foreach (SearchResult AdObj in search.FindAll())
{
foreach (String objName in AdObj.GetDirectoryEntry().Properties["member"])
{
str += Convert.ToString(objName) + "<Br>";
int selIndex = objName.IndexOf("CN=") + 3;
int selEnd = objName.IndexOf(",OU") - 3;
str1 += objName.Substring(selIndex, selEnd).Replace("\\", "") + "<BR>";
DirectorySearcher dsSearch = new DirectorySearcher(entry);
dsSearch.Filter = "CN=" + objName.Substring(selIndex, selEnd).Replace("\\", "");
foreach (SearchResult rs in dsSearch.FindAll())
{
str1 += "<p align='right'><font face='calibri' color='#2266aa' size=2>" + Convert.ToString(rs.GetDirectoryEntry().Properties["mail"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["displayName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["sAMAccountName"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["department"].Value) + "|" + Convert.ToString(rs.GetDirectoryEntry().Properties["memberOf"].Value) + "</font></p>";
}
}
}
Response.Write("<BR>" + str + "<Br>" + str1 + "<BR>");
}
catch (Exception ex)
{
Response.Write("--unable to fetch--<BR>" + ex.Message);
}
}
此处,search.findAll
会抛出DirectoryCOMException
说
语法
中的dn无效
我需要获取属于通讯组列表的所有电子邮件地址。
我确实尝试实现下面链接中详述的方法,但遗憾的是它不起作用: https://forums.asp.net/t/1224607.aspx?Displaying+Members+in+a+Distribution+List
感谢任何帮助。
感谢。
答案 0 :(得分:1)
您应该查看System.DirectoryServices.AccountManagement
(S.DS.AM)命名空间。这使得你的生活整个比旧的,更笨重DirectorySearcher
...
基本上,您可以定义域上下文并轻松在AD中查找用户和/或组:
// set up domain context - limit to the OU you're interested in
// use this constructor if you want just the default domain, and search in the whole domain
// using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null))
// or use this line here to define a *container* to search inside of
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, null, "OU=YourOU,DC=YourCompany,DC=Com"))
{
// find the group in question - this can be either a DL, or a security group - both should be found just fine
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
// if found....
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
// do whatever you need to do to those members
}
}
}
新的S.DS.AM让您可以轻松地与AD中的用户和群组一起玩!
在这里阅读更多相关信息:
另外:我强烈建议明确且严格地保持抓取数据并将显示分开 - 不要混用用HTML表示获取 - 这只是一个巨大的泥球&#34;样式代码 - 不 推荐!
一种方法可以检索您需要的数据,例如一个List<UserPrincipal>
(或者你也可以定义你自己的类来保存你需要的数据),然后有一个第二个,单独的方法从第一个方法获取这些信息并迭代它显示它。