Solved

C# get user from AD

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to apply a registry entry for all Citrix users 30 69
Application of a group policy 11 72
.NET Redemption/Outlook Examples 3 35
is a device online 4 46
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
A hard and fast method for reducing Active Directory Administrators members.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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