Solved

C# get user from AD

Posted on 2011-02-24
4
688 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
  • 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

Join & Write a Comment

Companies that have implemented Microsoft’s Active Directory need to ensure that the Active Directory is configured and operating properly. If there are issues found and not resolved, it eventually leads the components to fail or stop working and fi…
Introduction You may have a need to setup a group of users to allow local administrative access on workstations.  In a domain environment this can easily be achieved with Restricted Groups and Group Policies. This article will demonstrate how to…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now