locked
How to return a group of users in a list using active directory RRS feed

  • Question

  • User884139567 posted

    Hi, i'm new to asp.net and need some help. I need to list a group of users, their group name is (IRS Group) to a list group . Can some one pls show me how to do this. I'm very new to asp.net and dont have much knowlegde about acctive directory.

    Thanks a ton guys.


    Tuesday, June 15, 2010 11:32 AM

Answers

  • User-2080168305 posted


    public static
    DirectoryEntry GetDirectoryEntry(string DomainReference)
    {
    string ADFullPath = "LDAP://domain.com";
    DirectoryEntry de = new DirectoryEntry(ADFullPath + DomainReference, "
    username", "password", AuthenticationTypes.Secure);
    return de;
    }

    private static string GetLDAPDomain()
    {
    StringBuilder LDAPDomain = new StringBuilder();
    string[] LDAPDC = "
    domianNAme.com".Split('.');
    for (int i = 0; i < LDAPDC.GetUpperBound(0) + 1; i++)
    {
    LDAPDomain.Append("
    DC=" + LDAPDC[i]);
    if (i < LDAPDC.GetUpperBound(0))
    {
    LDAPDomain.Append("
    ,");
    }
    }
    return LDAPDomain.ToString();
    }



    if (CheckUserAuthentication(HttpContext.Current.User.Identity.Name.ToString()))
    {
    // success
    }
    else
    //deny access

    --------------------/* Checking wheather the user belongs to Your Group.*/------------------

    private bool CheckUserAuthentication(String userAccount)
    {


    DirectoryEntry entry = GetDirectoryEntry("
    /" + GetLDAPDomain());

    String account = userAccount.Replace(@"
    DomainName\", "");
    string group = "
    Group to Check";
    try
    {

    DirectorySearcher search = new DirectorySearcher(entry);
    search.Filter = "
    (SAMAccountName=" + account + ")";
    search.PropertiesToLoad.Add("
    memberOf");
    SearchResult result = search.FindOne();

    DirectorySearcher groupSearch = new DirectorySearcher(entry);
    groupSearch.Filter = "
    (SAMAccountName=" + group + ")";
    groupSearch.PropertiesToLoad.Add("
    member");
    SearchResult groupResult = groupSearch.FindOne();
    if (result != null)
    {
    int allGroupCount = result.Properties["
    memberOf"].Count;

    int checkGroupCount = groupResult.Properties["
    member"].Count;

    for (int i = 0; i < allGroupCount; i++)
    {
    string number = result.Properties["
    memberOf"][i].ToString();
    for (int j = 0; j < checkGroupCount ; j++)
    {
    if (number == groupResult.Properties["
    member"][j].ToString())
    {

    return true;
    }
    }
    }
    }
    else
    {
    return false;
    }
    }
    catch (Exception ex)
    {
    string debug = ex.Message;

    return false;
    }
    return false;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 15, 2010 12:27 PM