locked
User details of user(s) beloning to AD group RRS feed

  • Question

  • User-1829016774 posted

    Hi,

     

    I'm looking for some code to help me in an app I'm building. I'm looking for some code that will do the following (I'm using C# in Visual Studio 2008):

     

    - Find all users belonging to a given AD group.

    - Then using that list of names find each user's windows username, fullname and email address.

     

    I have played with the follwoing code but am falling short (IE not getting exactly what I need):

    ///////////////////////////////////////////////////// 

    SearchResult
    result;

    DirectorySearcher search = new DirectorySearcher();

    search.Filter = String.Format("(cn={0})", "ADGroupName");search.PropertiesToLoad.Add("member");

    result = search.FindOne();

    for (int counter = 0; counter < result.Properties["member"].Count; counter++)

    {

    string stringName = result.Properties["member"][counter].ToString();

    Response.Write("Your Full name is: " + stringName + "</BR>");

    }

     

    /////////////////////////////////////////////////////

     

    Thanks in advance

    Wednesday, May 6, 2009 12:02 AM

Answers

  • User-1829016774 posted

    Hi,

     

    Got what I needed. Thanks to  Asif for leading me in the right direction.

     

    Below is the code you'll need... It's maybe not as intricate as it should be, but none the less it's a building block... Sorry about the object naming... Just wanted to get this to work... Hope someone else finds this useful.

     

    Cheers

    ////////////////////////////////////////////////////////////////////////////

    DirectorySearcher
    srch = new DirectorySearcher();

    srch.Filter = "(cn=ADGroupName)";

    srch.PropertiesToLoad.Add("member");SearchResultCollection results;

    results = srch.FindAll();

    foreach (SearchResult resultPerson in results)

    {

    for (int counter = 0; counter < resultPerson.Properties["member"].Count; counter++)

    {

    string[] strUser = resultPerson.Properties["member"][counter].ToString().Split(',');

    string FindUser = strUser[0].ToString();

    srch.Filter = "(" + FindUser + ")";

    srch.PropertiesToLoad.Add("name");

    srch.PropertiesToLoad.Add("mail");

    SearchResultCollection resultsx;

    resultsx = srch.FindAll();

     

    foreach (SearchResult resultPersonx in resultsx)

    {

    string strFullname = resultPersonx.Properties["name"][0].ToString();

    string strEmail = resultPersonx.Properties["mail"][0].ToString();

    Response.Write(strFullname + " " + strEmail + "</BR>");

    }

    }

     

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2009 3:00 AM

All replies

  • User1153149830 posted

     Try this

    private string GetUserName()
    {
    string retVal = string.Empty;
    //Pull the username out of the domain\user string.
    retVal = Page.User.Identity.Name.Split('\\')[1];
     
    DirectoryEntry userEntry = new DirectoryEntry("WinNT://" + userName +",User");
    retVal = (string)userEntry.Properties["fullname"].Value;
    return retVal;
    }
     

     For more details visit this link:

    http://www.codeproject.com/KB/aspnet/adsi1.aspx

    Regards

    Asif

     

    Wednesday, May 6, 2009 12:54 AM
  • User-1829016774 posted

    Hi,

     

    Thanks for getting back so quickly.

     

    I've also got some code doing roughly what you've posted. The code I posted can get the users beloning to the group. But then once I have those it's a bit difficult to get the username (not sure how to format the results). I understand I can use the username to get the fullname, but what about the email address?

     

    Thanks in advance... again!

    Wednesday, May 6, 2009 1:16 AM
  • User-1829016774 posted

    Hi,

     

    Got what I needed. Thanks to  Asif for leading me in the right direction.

     

    Below is the code you'll need... It's maybe not as intricate as it should be, but none the less it's a building block... Sorry about the object naming... Just wanted to get this to work... Hope someone else finds this useful.

     

    Cheers

    ////////////////////////////////////////////////////////////////////////////

    DirectorySearcher
    srch = new DirectorySearcher();

    srch.Filter = "(cn=ADGroupName)";

    srch.PropertiesToLoad.Add("member");SearchResultCollection results;

    results = srch.FindAll();

    foreach (SearchResult resultPerson in results)

    {

    for (int counter = 0; counter < resultPerson.Properties["member"].Count; counter++)

    {

    string[] strUser = resultPerson.Properties["member"][counter].ToString().Split(',');

    string FindUser = strUser[0].ToString();

    srch.Filter = "(" + FindUser + ")";

    srch.PropertiesToLoad.Add("name");

    srch.PropertiesToLoad.Add("mail");

    SearchResultCollection resultsx;

    resultsx = srch.FindAll();

     

    foreach (SearchResult resultPersonx in resultsx)

    {

    string strFullname = resultPersonx.Properties["name"][0].ToString();

    string strEmail = resultPersonx.Properties["mail"][0].ToString();

    Response.Write(strFullname + " " + strEmail + "</BR>");

    }

    }

     

    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2009 3:00 AM