I'm trying to use Active Directory to get a list of users that are in the
same directory as the logon user. I'm trying to do this as a Web App written
in C# using Windows Security and Impersonation. When running from the local
host I have no problem; but when I try the same thing remotely I get the
error "System.Runtime.InteropServices.COMException" on the second hit on
Active Directory, the first hit works fine. I used to get the same error all
the time until I started using Impersonation.
Any suggestions as to what might be the issue?
Below is the specific code in question. It errors out at "DirectoryEntry
child"
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
DirectoryEntry user_entry = new
DirectoryEntry("LDAP://DOMAIN.org/DC=DOMAIN,DC=org");
DirectorySearcher mySearcher = new DirectorySearcher(user_entry);
Session["User.ID"] = User.Identity.Name.ToString().Substring(12);
mySearcher.Filter = ("(CN=" + Session["User.ID"].ToString() + ")");
SearchResult resEnt = mySearcher.FindOne();
Session["User.Name"] =
resEnt.GetDirectoryEntry().Properties["DisplayName"].Value.ToString();
Session["User.Path"] = resEnt.Path.ToString();
Session["User.CN"] =
resEnt.GetDirectoryEntry().Properties["CN"].Value.ToString();
lblWelcome.Text = "Welcome, " + Session["User.Name"].ToString();
string TempString = resEnt.Path.ToString().Replace("CN=" +
Session["User.CN"].ToString() + ",", "");
DirectoryEntry local_entry = new DirectoryEntry(TempString);
//this is where the error occurs
foreach(DirectoryEntry child in local_entry.Children)
{
if (child.Properties.Contains("Mail") == true)
{
string tempString1 = child.Properties["DisplayName"].Value.ToString();
string tempString2 = child.Properties["Mail"].Value.ToString();
ListItem tempItem = new ListItem(tempString1,tempString2);
ddlNominee.Items.Add(tempItem);
}
}
}
}