Click here to Skip to main content
15,886,199 members
Articles / Web Development / ASP.NET

Using Active Directory In ASP.NET - Enumerate Active Directory Users

Rate me:
Please Sign up or sign in to vote.
2.64/5 (18 votes)
10 Feb 20021 min read 229.2K   3.8K   78  
An article on using DirectoryServices namespace to enumerate Active Directory Users
// Disclaimer and Copyright Information
// ADSIObject.cs : 
//
// All rights reserved.
//
// Written by Pardesi Services, LLC
// Version 1.0
//
// Distribute freely, except: don't remove our name from the source or
// documentation (don't take credit for my work), mark your changes (don't
// get me blamed for your possible bugs), don't alter or remove this
// notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
//
// Send bug reports, bug fixes, enhancements, requests, flames, etc. to
// softomatix@pardesiservices.com
///////////////////////////////////////////////////////////////////////////////
//

using System;
using System.Diagnostics;
using System.Text;
using System.DirectoryServices;
using System.Xml;

namespace ASPNet_App
{
	/// <summary>
	/// 
	/// </summary>
	public abstract class ADSIObject
	{
		protected bool m_bInitialized = false;
		protected int m_nNumProperties = -1;
		protected XmlDocument m_obPropertiesDoc = null;
		public ADSIObject()
		{
			// 
			// TODO: Add constructor logic here
			//
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="propColl"></param>
		/// <returns></returns>
		public virtual bool Initialize(ResultPropertyCollection propColl)
		{
			// Check if the input collection object is null or not.
			if (null == propColl)
			{
				return false;
			}

			XmlNode obTopNode;
			try
			{
				// Create XMl Document to store values.
				m_obPropertiesDoc = new XmlDocument();
				obTopNode = this.AppendTopLevelNode();
				if (null == obTopNode)
				{
					Trace.WriteLine("Failed to add top level node to xml document");
					return false;
				}
				// Iterate over each key in the collection.
				foreach (string strKey in propColl.PropertyNames)
				{
					foreach (object obProp in propColl[strKey])
					{
						this.AppendPropertyNode(obTopNode, strKey, obProp);
					}
				}

				// Process the XML Document to store the information in local variables.
				this.ProcessUserData();

				this.m_nNumProperties = propColl.PropertyNames.Count;
				this.m_bInitialized = true;
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return false;
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public XmlDocument GetProperties()
		{
			if (!m_bInitialized)
			{
				return null;
			}

			return m_obPropertiesDoc;
		}

		/// <summary>
		/// 
		/// </summary>
		public int NumProperties
		{
			get
			{
				return m_nNumProperties;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		protected virtual XmlNode AppendTopLevelNode()
		{
			XmlNode obTopNode = null;
			if (this.m_obPropertiesDoc == null)
			{
				return null;
			}

			StringBuilder strTopNode = new StringBuilder();
			strTopNode.Append ("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
			strTopNode.Append ("<adsi_object> </adsi_object>");

			try
			{
				m_obPropertiesDoc.LoadXml(strTopNode.ToString());
				obTopNode = m_obPropertiesDoc.SelectSingleNode("//adsi_object");
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
				return null;
			}
			return obTopNode;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strNode"></param>
		/// <param name="prop"></param>
		/// <returns></returns>
		protected bool AppendPropertyNode(XmlNode node, string strNode, object prop)
		{
			try
			{
				XmlElement elem = this.m_obPropertiesDoc.CreateElement(strNode);
				XmlAttribute attrib = this.m_obPropertiesDoc.CreateAttribute("type");
				attrib.InnerText = prop.GetType().ToString();

				// Append the value type attribute to element.
				elem.Attributes.Append(attrib);

				// Append the value now...
				elem.InnerText = prop.ToString();

				// Append this element to top node.
				node.AppendChild(elem);
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
			}
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		protected virtual void ProcessUserData()
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="strQuery"></param>
		/// <param name="strVal"></param>
		protected void GetNodeValue(string strQuery, ref string strVal)
		{
			try
			{
				XmlNode node = this.m_obPropertiesDoc.SelectSingleNode(strQuery);
				strVal = node.InnerText;
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.Message);
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
To learn more about us, Please visit us at http://www.netomatix.com

Comments and Discussions