Solved

c# - active directory

Posted on 2014-03-04
6
371 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
ID: 39905365
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
ID: 39905430
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
ID: 39920059
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:josephm67
ID: 39920131
What is OU and DC?
0
 
LVL 16

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 250 total points
ID: 39925505
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
ID: 39978249
thanks
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

809 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