How to query the Active Directory to get the Department Name, Manager Name and Team members C#.Net / .Net 4.5

.Net has the most extensive libraries readily available to query the Active Directory Services. But most of the MSDNs nowadays talk more about the Azure Services to access the Active Directory. Below, I have specified, the easiest to get the Department Name, Manager Name, Team Members of an Employee.

I am using a ASP.Net Web Service written in .Net 4.5.

STEPS

1. Create a C#.Net Web Service.

2. Add the following references to your project:

  • System.DirectoryServices
  • System.DirectoryServices.AccountManagement

3. Create a new class by name PrincipalExtension and copy the below contents into it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

public static class PrincipalExtension
{
public static String GetProperty(this Principal principal, String property)
{
DirectoryEntry directoryEntry = principal.GetUnderlyingObject() as DirectoryEntry;
if (directoryEntry.Properties.Contains(property))
return directoryEntry.Properties[property].Value.ToString();
else
return String.Empty;
}

public static String GetCompany(this Principal principal)
{
return principal.GetProperty(“company”);
}

public static String GetDepartment(this Principal principal)
{
return principal.GetProperty(“department”);
}

public static String GetManager(this Principal principal)
{
return principal.GetProperty(“manager”);
}
}

4. Go to your default Service.cs class and then call the methods from your ASP.Net web methods, as shown below:

[WebMethod]
public string GetUserTeam(string asUserName)
{
string sTeamName = “”;

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, asUserName);

sTeamName = userPrincipal.GetDepartment();

return sTeamName;
}

[WebMethod]
public string GetUserTeamManager(string asUserName)
{
string sManagerCN = “”;
string sManagerName = “”;

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, asUserName);

string[] sArrSeparators = new string[] { “,” };
string[] sArrStringSplit;

sManagerCN = userPrincipal.GetManager();

if (sManagerCN != “”)
{

sArrStringSplit = sManagerCN.Split(sArrSeparators, StringSplitOptions.None);

sManagerName = sArrStringSplit[0].Remove(0, 3);

return sManagerName;
}
else
{
sManagerName = “”;
return sManagerName;
}
}

[WebMethod]
public string GetUserTeamMates(string asUserName)
{

string sManagerCN = “”;

string sManagerName = “”;

PrincipalContext domain = new PrincipalContext(ContextType.Domain);
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(domain, asUserName);

string[] sArrSeparators = new string[] { “,” };
string sConcatTeamMates = “”;

sManagerCN = userPrincipal.GetManager();

DirectorySearcher search = new DirectorySearcher();

search.Filter = “(&(objectClass=user)(manager=” + sManagerCN + “))”;
search.PropertiesToLoad.Add(“name”);

SearchResult result;
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
if (result.Properties.Contains(“name”))
{
sConcatTeamMates = sConcatTeamMates + “,” + (String)result.Properties[“name”][0];
}
}

if (sConcatTeamMates.Contains(“,”))
{
sConcatTeamMates = sConcatTeamMates.Remove(0, 1);
}
}

return sConcatTeamMates;

}

Good Luck!

Advertisements

Source Not Found Error Messages, while Stepping Into (F11) code in .Net debugger

You might encounter the “Source Not Found” Message, while you Step Into your code, while debugging. You will be seeing something like this.

String.cs

The Fix is simple!, just follow the below process

Visual Studio Version 2013 Update 4

1. Debug options (You will find it under Debug -> Options & Settings

Open options dialog and make sure you have check boxes set as on following image in red boxes.

debugoptions

2. Download symbols and source

Now we have to allow downloading symbol files. By default the location of symbols is somewhere under application user data folder. I prefer some location on some drive root usually.

debugsymbols

NB! I chose “All modules, unless excluded” to get everything from symbol server.

After clicking OK you see window like on following image. Make a cup of coffee and wait until symbols are downloaded. It takes a while. For me it took almost 15 mins 🙂

downloadingpublicsymbols

After it gets completely downloaded, you are good to go ahead and debug your source code!