Solved

c# - active directory

Posted on 2014-03-04
6
373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

687 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