c#


GroupPrincipal.GetMembers vs LDAP search to get all users from Ad Group including sub group


I am confused as to when one should choose GroupPrincipal.GetMembers(true)
over DirectorySearcher using filter when getting all users from groups including nested groups. One thing I noticed was there is no way of paginating in GroupPrincipal and this might cause performance issue when retrieving large users sets.
Here is the code to retrieve users (not exactly but fine for now) using both the api taken from SO
public IEnumberable<UserPrincipal> GetUsersFromGroups(string[] groupNames)
{
using (var ctx = new PrincipalContext(ContextType.Domain))
{
foreach (var groupName in groupNames)
{
foreach (var userPrincipal in GroupPrincipal.FindByIdentity(ctx, groupName)
.GetMembers(true))
{
yield return userPrincipal;
}
}
}
}
// vs Directory search
public static IEnumerable<DirectoryEntry> GetUsersFromGroups(string[] groupNames)
{
if (groupNames.Length > 0)
{
var searcher = new DirectorySearcher();
string searchFilter = "(&(objectClass=Group)"; //filter for groups
searchFilter += "(|"; //start a group of or parameters
foreach (var group in groupNames) //loop through the group names
{
searchFilter += string.Format("(SAMAccountName={0})",group); //add a parameter for each group in the list
}
searchFilter += "))"; //close off the filter string
searcher.Filter = searchFilter; //add the filter to the searcher
searcher.PropertiesToLoad.Add("member"); // load the members property for the group
var searchResults = searcher.FindAll(); // perform the search
foreach (SearchResult result in searchResults)
{
var directoryEntry = (DirectoryEntry)result.GetDirectoryEntry(); // get the directory entry for the group
PropertyValueCollection members = directoryEntry.Properties["member"]; // get the members collection
foreach (string name in members) //iterate through the members. this string will be the distinguished name
{
yield return new DirectoryEntry(string.Format("LDAP://{0}",name)); //return the directory entry. you may get the entry and return the display name or just return distinguished name.
}
}
}
}

Related Links

how to load a dll into ram and have it accessible by another dll that has been loaded into ram
Instance generic method by type with reflection C# [duplicate]
How to deal with DBNull in DataContractSerializer?
Inheritance Variable Questions (Changing inherited variables) in Unity (C#)
Unable to update main form control from subclass
On an ASP.NET HTML page with a DO/WHILE loop: How to stop then continue loop with SUBMIT button
How to have events separated at least by a given time span?
Getting error in Unity Universal Windows Platform
Can a non-nullable int be null? [duplicate]
Delete from one, add to another Listbox and vice versa with the same sorting like before
use unrar.dll in a c# project to retrieve the list of file and relative CRC code
How to declare an array containing generic type?
Manage multiple authentications with ASP.NET Identity
Is there any reason to keep a settings file nested underneath properties?
IIS Web Application can't be accessed remotely
re-number a value by 1 if same value

Categories

HOME
logging
system-calls
sip
gap-system
ibm
blob
azure-database-mysql
magento-1.9.2.1
angularjs-directive
docker-compose
markdown
runtime
sha1
reportlab
samba
loader
passbook
keystone
hololens
screen-scraping
aspxgridview
gulp-jshint
boosting
ipython-parallel
boost-log
restler
cocoa-scripting
ragel
border-layout
form-fields
armadillo
ng-lightning
aura-framework
amazon-rds-aurora
gradlew
flask-socketio
appcmd
vertex
webmatrix
androiddesignsupport
multi-targeting
mavlink
system-integration
boost-propertytree
jmh
innertext
custom-build-step
ideavim
app-data
winforms-interop
sparkcore
polarion
parseexception
beagleboard
data-quality-services
fiware-health
javadb
livescript
ios-enterprise
twitter-finagle
dateadd
nodeunit
connections
internet-radio
logo-lang
node-amqp
lastinsertid
qmainwindow
antixsslibrary
screensharing
linear-interpolation
orchestration
states
beep
django-admin-tools
wxperl
eeprom
motorola-droid
advanceddatagrid
digg
mismatch
file-processing
excellibrary
oaw
coderush-xpress

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App