473,598 Members | 3,486 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Populating a DDL with users from Active Directory

My goal, somehow, is to populate a dropdownlist with all the user names
in active directory. I don't even know where to begin, really.

I added a reference to System.Director yServices so I could use the
System.Director yServices.Activ eDirectory namespace. I don't even know if
this is the right way to go as I can't seem to find anything in that
namespace that would help me query active directory for names.

I can't use an LDAP query because lDAP isn't working on our network for
some reason (never has. i posted a note asking help for this on the
win2000.general newsgroup but no one replied).

So, even if I could use the framework to access active directory to get
a list of all the users, what about security? I don't think the aspnet
account on the web server has access to the domain's directory. How
would I go about specifying an account that could be used to access the
directory (if its necessary to do so)?

All this would need to be done in VB as I don't know C#. Oh, and I'm
using VS 2005 and .NET 2.0.

Thanks!
Jim
Jul 28 '06 #1
2 5957
>My goal, somehow, is to populate a dropdownlist with all the user names
>in active directory. I don't even know where to begin, really.
OK, the very FIRST question I would ask myself is : REALLY ?? Do you
really need this, and second, if yes - how many users will you be
displaying?? If you have a dozen or two - okay, no problem.

If you have more than 50 or so - forget it - won't work. Having huge
long lists of user name is just totally against all usability.
>I added a reference to System.Director yServices so I could use the
System.Directo ryServices.Acti veDirectory namespace. I don't even know if
this is the right way to go as I can't seem to find anything in that
namespace that would help me query active directory for names.
Sure, you're on the right track. Next thing you need to do is this:
since you have a domain with any number of potential organizational
units (OU's) inside it structuring your organization, you will need to
do a domain-wide search for users (if you really want that).

Again: an alternative might be to do a two-stage search. First present
the user with a list of all OU's in your domain (usually a couple or a
few dozen), e.g. "Finance", "R&D" and so forth. Once the user has
picked the OU he wants to see, then search the users inside that
(usually a MUCH smaller number than ALL the domain users) and shows
just those in a second dropdown.
>I can't use an LDAP query because lDAP isn't working on our network for
some reason (never has. i posted a note asking help for this on the
win2000.genera l newsgroup but no one replied).
Well, if you can't use LDAP, you're totally out of luck. Active
Directory and all the System.Director yServices stuff is based on LDAP.
No LDAP, no go........
So, once you've decided what to do and LDAP is working, here's how you
can do a domain-wide search - if you really want to do it in the end.

First you need to know what your domain is - either ask your domain
admin, or you can peek it like so (sorry, I'm fluent in C# only, so
you'll have to do the translations to VB.NET yourself or use one of
the C#-to-VB.NET translators out there on the 'net):

// bind to the LDAP root
DirectoryEntry root = new DirectoryEntry( "LDAP://RootDSE");

// grab the defaultNamingCo ntext
string myDomain = root.Properties["defaultNamingC ontext"].Value;

Next, once you have your domain name, you need to set up a domain
searcher on the domain level, and search for all users inside it:

DirectoryEntry domain = new DirectoryEntry( "LDAP://" + myDomain);

DirectorySearch er dsUsers = new DirectorySearch er(domain);

// set up the searcher properties - search users, and return
// just their distinguishedNa me, givenName (first name), and
// sn (surname = family name)

dsUsers.Filter = "(&(objectCateg ory=person)(obj ectClass=user)) ";

dsUsers.Propert iesToLoad.Add(" distinguishedNa me");
dsUsers.Propert iesToLoad.Add(" givenName");
dsUsers.Propert iesToLoad.Add(" sn");

Now let the searcher do its magic and find all users - you will get a
"SearchResu lt" object back, in which you can access all the properties
you've specified in the "PropertiesToLo ad" collection:

List<stringallD omainUsers = new List<string>();

foreach(SearchR esult result in dsUsers.FindAll () )
{
// grab the properties and store them somewhere, e.g. in a
// List<stringor something like that, which can then be
// bound to a combobox
string firstName = result.Properti es["givenName"][0].ToString();
string lastName = result.Properti es["sn"][0].ToString();

allDomainUsers. Add(firstName + " " + lastName);
}

Hope this helps you get started a bit.

If you have more questions, you can go to the MSDN
System.Director yServices portal here:

http://msdn.microsoft.com/library/de...sds/portal.asp

There are LOTS of really good examples there, in both C# and VB.NET.

Or buy the excellent book by my fellow MVP's Joe Kaplan and Ryan Dunn
- really excellent stuff about Active Directory programming!

The .NET Developer's Guide to Directory Services Programming
(Microsoft Net Development Series)
http://www.amazon.com/gp/product/0321350170/

Or post here again, of course - better yet - post in the
microsoft.publi c.adsi.general group, that's the hard-core Active
Directory programming group.

Marc
Aug 1 '06 #2
Marc Scheuner wrote:
>My goal, somehow, is to populate a dropdownlist with all the user names
in active directory. I don't even know where to begin, really.

OK, the very FIRST question I would ask myself is : REALLY ?? Do you
really need this, and second, if yes - how many users will you be
displaying?? If you have a dozen or two - okay, no problem.

If you have more than 50 or so - forget it - won't work. Having huge
long lists of user name is just totally against all usability.
>I added a reference to System.Director yServices so I could use the
System.Directo ryServices.Acti veDirectory namespace. I don't even know if
this is the right way to go as I can't seem to find anything in that
namespace that would help me query active directory for names.

Sure, you're on the right track. Next thing you need to do is this:
since you have a domain with any number of potential organizational
units (OU's) inside it structuring your organization, you will need to
do a domain-wide search for users (if you really want that).

Again: an alternative might be to do a two-stage search. First present
the user with a list of all OU's in your domain (usually a couple or a
few dozen), e.g. "Finance", "R&D" and so forth. Once the user has
picked the OU he wants to see, then search the users inside that
(usually a MUCH smaller number than ALL the domain users) and shows
just those in a second dropdown.
>I can't use an LDAP query because lDAP isn't working on our network for
some reason (never has. i posted a note asking help for this on the
win2000.genera l newsgroup but no one replied).

Well, if you can't use LDAP, you're totally out of luck. Active
Directory and all the System.Director yServices stuff is based on LDAP.
No LDAP, no go........
So, once you've decided what to do and LDAP is working, here's how you
can do a domain-wide search - if you really want to do it in the end.

First you need to know what your domain is - either ask your domain
admin, or you can peek it like so (sorry, I'm fluent in C# only, so
you'll have to do the translations to VB.NET yourself or use one of
the C#-to-VB.NET translators out there on the 'net):

// bind to the LDAP root
DirectoryEntry root = new DirectoryEntry( "LDAP://RootDSE");

// grab the defaultNamingCo ntext
string myDomain = root.Properties["defaultNamingC ontext"].Value;

Next, once you have your domain name, you need to set up a domain
searcher on the domain level, and search for all users inside it:

DirectoryEntry domain = new DirectoryEntry( "LDAP://" + myDomain);

DirectorySearch er dsUsers = new DirectorySearch er(domain);

// set up the searcher properties - search users, and return
// just their distinguishedNa me, givenName (first name), and
// sn (surname = family name)

dsUsers.Filter = "(&(objectCateg ory=person)(obj ectClass=user)) ";

dsUsers.Propert iesToLoad.Add(" distinguishedNa me");
dsUsers.Propert iesToLoad.Add(" givenName");
dsUsers.Propert iesToLoad.Add(" sn");

Now let the searcher do its magic and find all users - you will get a
"SearchResu lt" object back, in which you can access all the properties
you've specified in the "PropertiesToLo ad" collection:

List<stringallD omainUsers = new List<string>();

foreach(SearchR esult result in dsUsers.FindAll () )
{
// grab the properties and store them somewhere, e.g. in a
// List<stringor something like that, which can then be
// bound to a combobox
string firstName = result.Properti es["givenName"][0].ToString();
string lastName = result.Properti es["sn"][0].ToString();

allDomainUsers. Add(firstName + " " + lastName);
}

Hope this helps you get started a bit.

If you have more questions, you can go to the MSDN
System.Director yServices portal here:

http://msdn.microsoft.com/library/de...sds/portal.asp

There are LOTS of really good examples there, in both C# and VB.NET.

Or buy the excellent book by my fellow MVP's Joe Kaplan and Ryan Dunn
- really excellent stuff about Active Directory programming!

The .NET Developer's Guide to Directory Services Programming
(Microsoft Net Development Series)
http://www.amazon.com/gp/product/0321350170/

Or post here again, of course - better yet - post in the
microsoft.publi c.adsi.general group, that's the hard-core Active
Directory programming group.

Marc
Very, very informative, Marc. Thanks.

I will probably be able to translate your C# code to vb without too much
trouble. It's all similar in a way.

I helped build the domain here so I'm really familiar with the
structure. We have probably around 250 user accounts so, yea, it's way
above your recommendation of just a few dozen.

The whole purpose of this is, well, as silly as it may seem (not my deal
but asked to be done by me), is this:

The employee satisfaction workgroup has come up with this 'rock' called
the 'you rock award'. The ESQ committe wants a page on our internal
website that shows a list of all those who have had 'the rock'. In order
to take the work out of me or anyone else in the IS staff, we (my
co-worker and I) figured it would be a good idea if the individual
employee controlled what happens. After a week, the employee could hand
the rock off to someone else who the employee thinks deserves it. They
would then go to the internal website and they could use a drop down
list which would show all employess in the agency to pick the employee
the just gave the award to then click a submit button. Once they did
that, the new person would be added as the newest record in a database.
The newest record in the database would be added the top of a list on a
page which shows they got the award and when. The person in the list
right below their name is the person who gave them the award. Now, with
the new person the last record, the home page on the internal website
would now show the same drop down list of AD users so when the newly
seclected employee decided to pass on the award, they could now repeat
the same process. The person that gave them the award would, once they
gave the award away, would no longer have access to this drop down list.

The reason I wanted a drop down list is because it would be easier to
control the real AD username when using the system. The person giving
the award out may not know the actual AD username (ex. Jon Smith being
jsmith or jonsmi) and without that, it wouldn't be possible to control
the drop down list being displayed on the home page for just the person
show currently holds the award.

Feww. I hope all that made sense.

The employees wouldn't really know how to traverse the OUs in AD. No one
ever queries it directly in any way, not even through My Network Places.
The only time anyone sees a list of names is when they look through
the exchange address book in outlook.

Anyway. I think I have enough info to go forward, one way or another.

OH, and I think LDAP will work. I was trying to use WAB.exe to look at
the directory, which wouldn't work, as I was told in the active
directory news group.

Once again, Thanks Marc.
Aug 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1532
by: Ouaes Jamali | last post by:
Hi all, I am trying to set the "Filter" property of directory search object so that the search returns only users that are not disabled in Active Directory? Currently the way i do this to make a query to Active Directory and, have a if condition that looks for the "AccountDisabled" flag of ActiveDs.IADsUser object to find if the user account is disabled, but i want to move away from this and do the filter before i run the query to Active...
3
291
by: Jay | last post by:
I have an asp.net application that needs to fill a series of checkboxes with usernames from a particular group in Active Directory. Could someone please point me in the right direction in regards to a snippet of code or a link to where I might info on this? I've checked msdn and can find nothing definate. Thanks in advance, Jay
7
5210
by: Sync Walantaji | last post by:
Hi, I would like to write a asp.net winform program to authenticate users on Active Directory. Can I do this with asp.net if the IIS server is not part of the Active directory domain? Is there a working example link that you can point me to? Thanks
3
3465
by: Luis Esteban Valencia | last post by:
Hello gusys, Is it possible to make my asp.net application add users to the AD , I also want to be able to delete users, modify their information, everything through a website. Thanks for the info
1
3890
by: tangus via DotNetMonster.com | last post by:
Hello all, I'm really struggling with getting some Active Directory code to work in ASP.NET. Can you please provide assistance? I am executing the following code: Dim enTry As DirectoryEntry = New DirectoryEntry("LDAP://domain") Dim mySearcher As New DirectorySearcher(enTry) Dim resEnt As SearchResult mySearcher.Filter = ("(objectClass=*)") mySearcher.SearchScope = SearchScope.Subtree
6
2135
by: Mr Newbie | last post by:
Hi, Im in a situation where I need to restrict users, but I dont have access or wont be allowed access to manage groups in the domain. How can I restrict access is this case ? -- Best Regards
3
2238
by: Chakkaradeep | last post by:
Hi all, How to retrieve Users Names and also restrict internet access to a specific users using C# ? Thanks. Regards, C.C.Chakkaradeep
2
12005
by: j.m.osterman | last post by:
I haven't found exactly what I've been trying to do. All I am trying to do for now is just display usernames from Active Directory into a ListBox control on a page. I have found some code however that will just capture a Full Name from Text Box and display information from the Active Directory into a DataTable. I will display that code below.
5
4346
by: ashok90 | last post by:
hi Experts, I want to use Active Directory with C# .Net. My Problem is that i want to restrict some users on my system(which is not on any network) from accessing some applications like oracle, adobe photoshop. How can we do this programmaticaly through C# with the hep of active directory. How can we grant or revoke permissions from users through Active Directory. I simply want to assign different users different permissions on...
0
7902
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8395
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6719
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3898
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.