Solved

C# get user from AD

Posted on 2011-02-24
4
691 Views
Last Modified: 2012-05-11
Hello,

I am trying to create an easy console one button, one textbox. If I click the button the display name will appear in text box. I added "using System.DirectoryServices;". There could be hardcoded the samaccountname. This is only for me to understand how to get these parameters in C#.

And how to manage it in:

private void button1_Click(object sender, EventArgs e)
        {
            DirectorySearcher Dsearch = new DirectorySearcher(entry);
            DSearch.Filter = "(&(objectClass=user)(l=" + Name + "))";
            ???
            textbox1.text = ??
        }

Many Thanks
0
Comment
Question by:Radim88
[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
  • 2
  • 2
4 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34970413
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			DirectorySearcher searcher = new DirectorySearcher();
			searcher.Filter = "(&(objectClass=user)(sAMAccountName=tgerbert))";

			Console.WriteLine("Matching entries:");
			foreach (SearchResult result in searcher.FindAll())
			{
				DirectoryEntry entry = result.GetDirectoryEntry();
				Console.WriteLine(entry.Properties["displayName"].Value.ToString());
			}

			Console.ReadKey();
		}
	}
}

Open in new window

0
 

Author Comment

by:Radim88
ID: 34970482
Would be possible to put it into:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace GetUserFromAD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 125 total points
ID: 34970606
Same code, just change Console.WriteLine to MessageBox.Show, etc.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace GetUserFromAD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
	DirectorySearcher searcher = new DirectorySearcher();
	searcher.Filter = "(&(objectClass=user)(sAMAccountName=tgerbert))";

	foreach (SearchResult result in searcher.FindAll())
	{
		DirectoryEntry entry = result.GetDirectoryEntry();
		MessageBox.Show(entry.Properties["displayName"].Value.ToString());
	}
        } 

Open in new window

0
 

Author Closing Comment

by:Radim88
ID: 34970642
Thx
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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

749 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