Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Active Directory issue with Web not Window Form

Status
Not open for further replies.

CodingIsFun

Programmer
Apr 9, 2004
134
US
Hi all experts:

I am trying to figure out why this works in a windows form and not in a web application. I am trying to fetch all the groups for a user based on their email address. When I run the following code it works the way I expect it to:


using System;
using System.DirectoryServices;
using System.Collections;

namespace ActiveDirectoryUsers
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.Write("Enter the Users Email : ");
string email =Console.ReadLine();


DirectoryEntry entry = new DirectoryEntry("LDAP://MyDomain");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=user)");
mySearcher.Filter = ("(Mail=" + email + ")");
SearchResult resEnt = mySearcher.FindOne();
try
{
DirectoryEntry de = resEnt.GetDirectoryEntry();
object groups = de.Invoke("Groups");
foreach ( object group in (IEnumerable)groups)
{
// Get the Directory Entry.
DirectoryEntry groupEntry = new DirectoryEntry(group);
Console.WriteLine("Group: " + groupEntry.Name);
}
Console.WriteLine("Display Name : " + de.Properties["DisplayName"].Value.ToString());
Console.WriteLine("First Name : " + de.Properties["GivenName"].Value.ToString());
}
catch(Exception e)
{
Console.WriteLine("The following error occurred: " + e.Message);
}
}

}
}

When I try to use the following in a web application the resEnt is always empty, not defined or null. This is the code from my web application:

using System;
using System.DirectoryServices;
using System.IO;
using System.Collections;

namespace active_directory_test.classes
{
/// <summary>
/// Summary description for acitve_directory.
/// </summary>
public class acitve_directory
{
public acitve_directory()
{
}

public string get_user_groups_by_name(string user_email)
{

string group_list = "";

DirectoryEntry entry = new DirectoryEntry("LDAP://MyDomain");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=user)");
mySearcher.Filter = ("(Mail=" + user_email + ")");
SearchResult resEnt = mySearcher.FindOne();
try
{
DirectoryEntry de = resEnt.GetDirectoryEntry();
group_list = group_list + "Display Name : " + de.Properties["DisplayName"].Value.ToString() + "<br>";
group_list = group_list + "First Name : " + de.Properties["GivenName"].Value.ToString() + "<br>";

object groups = de.Invoke("Groups");
foreach ( object group in (IEnumerable)groups)
{
// Get the Directory Entry.
DirectoryEntry groupEntry = new DirectoryEntry(group);
group_list = group_list + "Group: " + groupEntry.Name + "<BR>";

}
}
catch(Exception e)
{
Console.WriteLine("The following error occurred: " + e.Message);
}

return group_list;
}

}
}
Do I need to do something different for a web application, Or am I missing some sort of permission to communicate with the active directory?

Thanks in advance..
 
I have figured out the problem, the web application needs to be configured with login information, because IIS is not impersonating a valid AD user for login authentication.

For other users reference just put in the following for the initial DirectoryEntry("LDAP://MyDomain","user","password",AuthenticationTypes.Secure)

thanks to everyone that tried to figure this out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top