Getting groups in ADS




Getting groups to which the user belongs in ADS

Getting groups to which the user belongs in ADS makes the admin realize , how appropriately the users are assigned to their relevant groups. All and all , management of users and groups and their roles is the key ingredient in Active Directory Services.

Active Directory (AD) is a directory service that Microsoft developed for Windows domain networks and is included in most Windows Server operating systems as a set of processes and services.
An AD domain controller authenticates and authorizes all users and computers in a Windows domain type network—assigning and enforcing security policies for all computers and installing or updating software. For example, when a user logs into a computer that is part of a Windows domain, Active Directory checks the submitted password and determines whether the user is a system administrator or normal user.reference wikipedia.org

using System.DirectoryServices;

public class test
{

private void main()
{
foreach (string @group in GetGroups())
{
Debug.Print(@group);
}
}

public List GetGroups()
{
DirectoryEntry objADAM = default(DirectoryEntry);
// Binding object.
DirectoryEntry objGroupEntry = default(DirectoryEntry);
// Group Results.
DirectorySearcher objSearchADAM = default(DirectorySearcher);
// Search object.
SearchResultCollection objSearchResults = default(SearchResultCollection);
// Results collection.
string strPath = null;
// Binding path.
List result = new List();

// Construct the binding string.
strPath = “LDAP://stefanserver.stefannet.local”;
//Change to your ADserver

// Get the AD LDS object.
try
{
objADAM = new DirectoryEntry(strPath);
objADAM.RefreshCache();
}
catch (Exception e)
{
throw e;
}

// Get search object, specify filter and scope,
// perform search.
try
{
objSearchADAM = new DirectorySearcher(objADAM);
objSearchADAM.Filter = “(&(objectClass=group))”;
objSearchADAM.SearchScope = SearchScope.Subtree;
objSearchResults = objSearchADAM.FindAll();
}
catch (Exception e)
{
throw e;
}

// Enumerate groups
try
{
if (objSearchResults.Count != 0)
{
foreach (SearchResult objResult in objSearchResults)
{
objGroupEntry = objResult.GetDirectoryEntry;
result.Add(objGroupEntry.Name);
}
}
else
{
throw new Exception(“No groups found”);
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}

return result;
}

}

Attribute:http://www.codeproject.com/Articles/3892/How-to-get-the-list-of-groups-that-a-user-is-a-mem

Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Leave a Reply