Solved

c# - active directory

Posted on 2014-03-04
6
362 Views
Last Modified: 2014-04-04
Hi,

I need help to access Active directory. I want to get info of user like Display name/first name etc.

I checked few links but I cannot understand how to define LDAP so I can get user info.

Please guide me about it.

Thanks
0
Comment
Question by:josephm67
6 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
Exactly what problem do you have with defining LDAP ? or see this guide and see if it helps :

http://ianatkinson.net/computing/adcsharp.htm
0
 
LVL 7

Accepted Solution

by:
Kishan Zunjare earned 250 total points
Comment Utility
To access active directory in C# ;

System References
Make sure you have included the following namespaces in your code:
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;

Open in new window

Directory Entry Object
DirectoryEntry dir = new DirectoryEntry("LDAP://your_domain_name");

Open in new window

Creating a Search Object and Executing the Search
The DirectorySearcher object searches the Active directory. You can set the filter property to retrieve specific records. I am also using the AND "&" property to combine two conditions.
DirectorySearcher search = new DirectorySearcher(dir);
search.Filter = "(&(objectClass=user)(givenname=First_Name))";

Open in new window

Search Results
First Create a SearchResult object to get the data from the search
SearchResult searchresult = search.FindOne(); // You can also use the FindAll() method for multiple objects.

   if (searchresult != null)
   {
	foreach(System.Collections.DictionaryEntry direntry in searchresult.Properties) 
                    TextBox1.Text += direntry.Key.ToString() +"\n"; // This will give you all the property names that are set for that particular object 		      

        TextBox1.Text += searchresult.GetDirectoryEntry().Properties["sn"].Value.ToString(); // Here displaying the lastname/surname in simple textbox.
   }

Open in new window


Hope this helps
-Kishan
0
 

Author Comment

by:josephm67
Comment Utility
Hi Kishan,

I tried the code but it is showing me below error:

The server is not operational.

I am trying to access it remotely. The computer where code is running does not have active directory. So, I guess we need to add some credential too or may be IP address as well.

Thanks
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:josephm67
Comment Utility
What is OU and DC?
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 250 total points
Comment Utility
Hi,

I just understand that you are trying to retrieve information about users from AD.

(1) About the error - The server is not operational. This error means your application is not able to talk to AD Server for any reason. The basic cause could be that you are not connected to AD Server directly.

(2) About LDAP connection string - I remember when I wrote program for interaction with AD as purpose of Window Authentication.

So here is the LDAP connection string -

<connectionStrings>
            <add name="ADConnection" connectionString="LDAP://DCRJSO1.DS.XX/OU=USERS,OU=RJSO,OU=MARKETING,DC=DS,DC=XX"/>            
      </connectionStrings>

The bold part in connection string is called Domain Controller of AD. To get it use link - How to find out which Domain Controller my PC is talking to?

The LDAP connection string should be read from Right to Left. So in our case it is saying that connect to DC (domain component) with dots(.) as XX-->DS.

And go to OU (Organizational UnitName) (to understand consider them as sub-directories) named as Users in Rjso in Marketing.

(3) Connect to AD - Code snippet for same.

using System.DirectoryServices;

// adUser is administrator user of AD
// adPass is password for same admin  user
// conStringPath - LDAP connection string

DirectoryEntry AD = new DirectoryEntry(conStringPath, adUser, adPass, AuthenticationTypes.Secure);
        
            using (DirectorySearcher ds = new DirectorySearcher(AD))
            {
                   foreach (DirectoryEntry de in AD.Children)
                {
                    // CN - DisplayName and sAMAccountName - UserLogon
                    logon = de.Properties["sAMAccountName"].Value.ToString();
                    name = de.Properties["CN"].Value.ToString().ToUpper();
                }
            }
        

Open in new window


Note - I bear no responsibility for any issues caused due to code or any part.
0
 

Author Closing Comment

by:josephm67
Comment Utility
thanks
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now