Sha1395
asked on
Retrieve active user from Active Directory using C#
So far i change "DHAEST" code to modifed based on my need.
Class Library File
Am not yet tested this one but i have a doubt while return the output,bcoz am going to return bunch of active users.
Does this code will return the value of all the users ?
Class Library File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;
namespace ActiveDir
{
public class Employee
{
// instance variables
public String useraccountcontrol;
// ...
// Constructors
public String FullName()
{
return String.Format("{0} ", useraccountcontrol).Trim();
}
public static implicit operator String(Employee value)
{
return value.ToString();
}
public Employee()
{
}
}
public class ActiveDirSearch
{
public ActiveDirSearch()
{ }
public Employee SearchUser()
{
Employee employee = new Employee();
try
{
DirectoryEntry entry = new DirectoryEntry("GC://Test", "Test\\Test", "Ld@Test", AuthenticationTypes.Secure);
System.DirectoryServices.DirectorySearcher search = new System.DirectoryServices.DirectorySearcher(entry);
search.Filter = "(useraccountcontrol=" + "512" + ")";
SearchResult result = search.FindOne();
if (result != null)
{
// user exists, cycle through LDAP fields (cn, telephonenumber etc.)
ResultPropertyCollection fields = result.Properties;
foreach (String ldapField in fields.PropertyNames)
{
// cycle through objects in each field e.g. group membership
// (for many fields there will only be one object such as name)
foreach (Object myCollection in fields[ldapField])
{
if (ldapField == "cn")
employee.useraccountcontrol = myCollection.ToString();
}
}
}
else
{
return null;
}
}
catch (Exception e)
{
throw e;
}
return employee;
}
}
}
Am not yet tested this one but i have a doubt while return the output,bcoz am going to return bunch of active users.
Does this code will return the value of all the users ?
As written you're only going to get one user.
ASKER
Exactly that's what i think so,so i have to use "Find All()" right ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
If you could explain little bit about this will help me to understand
(userAccountControl:1.2.84 0.113556.1 .4.803:=2) ))
thanks
If you could explain little bit about this will help me to understand
(userAccountControl:1.2.84
thanks
ASKER
No i got it thru MSDN article ,please ignore my previous question.
How can i return the result as an object and am going to call from Windows Application thru button click.
How can i return the result as an object and am going to call from Windows Application thru button click.
Put the directory searching code in it's own method, call that method from your button click event handler. Easy thing to do would be to simply return the SearchResultCollection:
Or, you can return an array of strings that has the user names:
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;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SearchResultCollection activeUsers = GetActiveUsers();
activeUserseTextBox.Clear();
if (activeUsers == null)
MessageBox.Show("No Active Users Found");
else
{
foreach (SearchResult user in activeUsers)
{
activeUserseTextBox.AppendText(user.GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString() + "\r\n");
}
}
}
private SearchResultCollection GetActiveUsers()
{
SearchResultCollection searchResults = null;
using (DirectorySearcher searcher = new DirectorySearcher())
{
searcher.Filter = "(&(objectCategory=person)(sAMAccountName=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
searchResults = searcher.FindAll();
}
if (searchResults.Count > 0)
return searchResults;
else
return null;
}
}
Or, you can return an array of strings that has the user names:
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;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] activeUsers = GetActiveUsers();
activeUserseTextBox.Clear();
if (activeUsers == null)
MessageBox.Show("No Active Users Found");
else
{
foreach (string user in activeUsers)
activeUserseTextBox.AppendText(user + Environment.NewLine);
}
}
private string[] GetActiveUsers()
{
string[] userList = null;
using (DirectorySearcher searcher = new DirectorySearcher())
{
searcher.Filter = "(&(objectCategory=person)(sAMAccountName=*)(!(userAccountControl:1.2.840.113556.1.4.803:=2)))";
SearchResultCollection searchResults = searcher.FindAll();
if (searchResults.Count > 0)
{
userList = new string[searchResults.Count];
for (int i = 0; i < userList.Length; i++)
userList[i] = searchResults[i].GetDirectoryEntry().Properties["sAMAccountName"].Value.ToString();
}
}
return userList;
}
}
ASKER
Thanks a lot tgerbert.