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

Multiple ItemSources binding
Difference between Do/While and While/Do
How to get the SQL code generate and return code
Crash in production when using a WebBrowser inside a Pivot
Add same value into string multiple times
Checking for Nulls on DB Record Mapping
Check InnerException in System.TypeInitializationException thrown by an external code frame
Datagridview using excel file as datasource in c#
Accessing anonymous type variables
The provider is not compatible with this version of oracle client. ASP.net and oracle issue
Visual Studio 2013 Solution building not in build order
Combine WindowsAuthentication with rules stored in DB
I18N Rendering as EN-US and not Neutral Culture
Using generic or nor knowing type of a property in a method
Mapping DTO classes with Expression<Func> inside another Expression<Func>
Multi-Store development

Categories

HOME
gcc
ssh
ldap
xamarin.forms
opencv4android
assign
redhawksdr
browser-cache
psexec
atlassian-fisheye
genexus
ejb-3.0
google-app-maker
google-search-appliance
desktop-application
http-status-codes
cakephp-2.0
element
openstack-horizon
background-image
summernote
descriptor
klipfolio
screen-scraping
prepros
expressionengine
traveling-salesman
adobe-captivate
scripting-language
request-uri
spring-data-gemfire
lines
tikz
mplab
jsonapi-resources
traitsui
mootools
cocoa-scripting
maven-2
console.readline
stdin
byobu
ng-lightning
idea-plugin
tapply
jvisualvm
php-mongodb
web-technologies
binary-decision-diagram
firefox-developer-edition
head
python-venv
gridgain
ocpjp
spring-data-commons
serverless-architecture
monaca
cloudconvert
riemann
aws-rds
multi-targeting
node.js-client
disassembly
nls
recursive-query
symantec
pebble-js
jquery-draggable
sdkman
play-json
gitk
self-join
jsoncpp
clearquest
android-networking
tidesdk
beagleboard
asp.net-2.0
pymol
esri-arc-engine
frisby.js
zoneminder
javacompiler
mdp
google-oauth-java-client
tlbimp
violation
wpd
google-checkout
uikit-dynamics
mysql2
boost.build
algol
metapost
ncml
volatility
motordriver
big-endian
sup
dancer
nsscrollview
django-admin-tools
auto-compile
disk-based
httpbrowsercapabilities
ironpython-studio

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