CodingIsFun
Programmer
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 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..