Solved

c# - active directory

Posted on 2014-03-04
6
370 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hey!!!! 1 22
VB.Net - MemoryMappedFiles - Confirm receipt 2 19
Get distinct values from excel or comm seperated file 4 27
Calculate number of nights between two dates 5 26
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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