Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# get user from AD

Posted on 2011-02-24
4
Medium Priority
?
694 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 500 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

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…
Here's a look at newsworthy articles and community happenings during the last month.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

670 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