Alright, I’ve gotten a lot of questions from people who aren’t familiar writing code on how to actually implement my active directory search web service I posted a long time ago. So here is a fully working sample. This is a self-contained ASP.NET 2.0 page that will search through the current domain it belongs too and return user’s FullName and Phone number as defined in there Active Directory profile. Only change that needs to be made is change:
protected void ldapsearch() {
string adPath = "LDAP://DOMAIN.COM"; //replace with your domain name, to restrict the search to a single OU tree use full LDAP notation ie: "LDAP://ou=Sales,dc=MyDomain,dc=com"
Continue on to see the full code, just copy and paste it into an ASPX file and throw it up on your iis server with ASP.NET 2.0 turned on.
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Assembly Name="System.DirectoryServices, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%@ Import Namespace="System.Text" %><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.1//EN” “http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd”>
<html xmlns="http://www.w3.org/1999/xhtml”>
<head runat="server”>
<title>Untitled Page</title>
<script language="C#” runat="server”>
private void Page_Load(object sender, System.EventArgs e){
//see’s if page has posted back information or button click
if(!Page.IsPostBack)
// starts new event for button click
this.Button1.Click += new System.EventHandler(this.Button1_Click);
}
// button calls this to start ldapsearch and send ajax back to button
public void Button1_Click(object sender, System.EventArgs e) {
ldapsearch();
}
protected void ldapsearch() {
string adPath = “LDAP://DOMAIN.COM”;
string query = TextBox1.Text+“*”;
ArrayList finalResults = new ArrayList();
// Search adPath using this string
string sFilter = String.Format(“(&(objectClass=user)(Name=*{0}*))”, query);using (DirectoryEntry de = new DirectoryEntry(adPath))
{
//these are the attributes that will show
string[] attribs = new string[]{“Name”, “TelephoneNumber”};
// Search using these filters
DirectorySearcher ds = new DirectorySearcher(de, sFilter, attribs);
using (SearchResultCollection src = ds.FindAll()) {
int i = 0;
while (i < src.Count) {
// if no results return say...
SearchResult sr = src;
/* if(SearchResult == null) {
msg.Text = “User has not been hired yet they will be hired in 2years 3weeks 24days 17seconds”;
} */
foreach (string key in attribs) {
if(sr.Properties.Contains(key)) {
foreach(object o in sr.Properties[key]) {
StringBuilder sb = new StringBuilder();
sb.AppendFormat(“{1} <hr />”, key, o);
finalResults.Add(sb.ToString());
}
}
}
StringBuilder listResults = new StringBuilder();
foreach (string item in finalResults)
{
listResults.Append(item);
}
msg.Text = listResults.ToString();
i++;
}
}
}
}</script>
</head>
<body>
<form autocomplete="off” id="form1” runat="server”>
<div style="width:125px;”>
<asp:TextBox ID="TextBox1” OnTextChanged="Button1_Click” runat="server” Width="120px”></asp:TextBox> <asp:Button ID="Button1” runat="server” onclick="Button1_Click” Text="Search” />
<asp:Panel id="Panel1” runat="server”><asp:Label id="msg” runat="server”></asp:Label> </asp:Panel>
</div>
</form>
</body>
</html>
Comments: (0)


















Post Tags: active directory code
Next entry: New Side Project Previous entry: SharePoint Data View Tricks