?
Solved

c# - active directory

Posted on 2014-03-04
6
Medium Priority
?
375 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 1000 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

Author Comment

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

Assisted Solution

by:Vikram Singh Saini
Vikram Singh Saini earned 1000 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

Technology Partners: 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!

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

771 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