Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# get user from AD

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

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Question has a verified solution.

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

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

972 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