<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ad hoc Geek &#187; Active Directory</title>
	<atom:link href="http://www.adhocgeek.com/tag/active-directory/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adhocgeek.com</link>
	<description>Approaching geekery in an ad hoc and improvisational manner.</description>
	<lastBuildDate>Fri, 30 Sep 2011 09:43:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Enumerating Users with LDAP</title>
		<link>http://www.adhocgeek.com/2009/10/enumerating-users-with-ldap/</link>
		<comments>http://www.adhocgeek.com/2009/10/enumerating-users-with-ldap/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 10:25:20 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[Active Directory]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[LDAP]]></category>
		<category><![CDATA[Starting Blocks]]></category>

		<guid isPermaLink="false">http://www.adhocgeek.com/?p=153</guid>
		<description><![CDATA[I&#8217;m sure there are lots of other tutorials, blog posts and actual documentation telling you how to do this, and this probably goes for a lot of the things I&#8217;ll post here, but this blog isn&#8217;t necessarily for other people. I&#8217;m beginning to realise that a lot of the code I post here is simply [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure there are lots of other tutorials, blog posts and actual documentation telling you how to do this, and this probably goes for a lot of the things I&#8217;ll post here, but this blog isn&#8217;t necessarily for other people. I&#8217;m beginning to realise that a lot of the code I post here is simply for my own benefit, and if it happens to coincide with the needs of someone else, that&#8217;s fine.</p>
<p>Anyway, I&#8217;m currently on-site again so I don&#8217;t have a lot of time to play and post snippets, but the LDAP code I was toying with this morning struck me as something I might want to revisit at some point in the future. It&#8217;s not exactly rocket science, but I often find that a short piece of working code can be an essential starting point when embarking on the understanding of a complex new library (for example, knowing that the technology is called LDAP and even understanding what that means, doesn&#8217;t tell you which .NET assemblies you should be investigating).</p>
<p>The following code is a C# console application which simply enumerates the users that the current account has access to know about in the given domain. An administrator running this should see everyone, but a lowly user with minimal rights may see only themselves (or possibly nothing at all!).</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.DirectoryServices</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ADTest
<span style="color: #008000;">&#123;</span>
  <span style="color: #6666cc; font-weight: bold;">class</span> Program
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
      DirectoryEntry entry <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DirectoryEntry<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;LDAP://FM&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      DirectorySearcher search <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DirectorySearcher<span style="color: #008000;">&#40;</span>entry<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> 
      search<span style="color: #008000;">.</span><span style="color: #0000FF;">Filter</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;(&amp;(objectClass=user)(objectCategory=person))&quot;</span><span style="color: #008000;">;</span>
      SearchResultCollection src <span style="color: #008000;">=</span> search<span style="color: #008000;">.</span><span style="color: #0000FF;">FindAll</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>src<span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>SearchResult sr <span style="color: #0600FF; font-weight: bold;">in</span> src<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> props <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> propName <span style="color: #0600FF; font-weight: bold;">in</span> sr<span style="color: #008000;">.</span><span style="color: #0000FF;">Properties</span><span style="color: #008000;">.</span><span style="color: #0000FF;">PropertyNames</span><span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
          props<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>propName <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;=[&quot;</span> <span style="color: #008000;">+</span> sr<span style="color: #008000;">.</span><span style="color: #0000FF;">Properties</span><span style="color: #008000;">&#91;</span>propName<span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;]&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;, &quot;</span>, props<span style="color: #008000;">.</span><span style="color: #0000FF;">ToArray</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;<span style="color: #008080; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This immediately gives me something to play with, it tells me I should investigate <strong>System.DirectoryServices</strong>, it tells me something about the way active directory search results are organised, and it tells me there is a curious filter syntax to investigate&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adhocgeek.com/2009/10/enumerating-users-with-ldap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

