<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Shier Vermont's Blog</title>
	<atom:link href="http://shiervermont.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://shiervermont.wordpress.com</link>
	<description>All about Shier Vermont</description>
	<lastBuildDate>Tue, 26 Aug 2008 02:47:20 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='shiervermont.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/c7ba4098d22c86ec809503ca300a6955?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Shier Vermont's Blog</title>
		<link>http://shiervermont.wordpress.com</link>
	</image>
			<item>
		<title>Authenticating and Retrieving Users Full Name via LDAP using ASP.Net 2.0</title>
		<link>http://shiervermont.wordpress.com/2008/03/19/authenticating-and-retrieving-users-full-name-via-ldap-using-aspnet-20/</link>
		<comments>http://shiervermont.wordpress.com/2008/03/19/authenticating-and-retrieving-users-full-name-via-ldap-using-aspnet-20/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 07:08:17 +0000</pubDate>
		<dc:creator>shiervermont</dc:creator>
				<category><![CDATA[Technologies]]></category>

		<guid isPermaLink="false">http://shiervermont.wordpress.com/?p=3</guid>
		<description><![CDATA[Hi All,
I hope this will help you a lot in authenticating and retrieving user&#8217;s full name via LDAP using ASP.Net 2.0.
Required Namespace:
using System.DirectoryServices; 
Authenticate Method:
public bool Authenticate(string userName, string password)
{
  DirectoryEntry deSystem = null;
  object obj = null;
  DirectorySearcher dsSystem = null;
  SearchResult srSystem = null;  bool isAuthenticated = false;  try
    {    // Initialize the Directory Entry [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shiervermont.wordpress.com&blog=1200712&post=3&subd=shiervermont&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Hi All,</p>
<p>I hope this will help you a lot in authenticating and retrieving user&#8217;s full name via LDAP using ASP.Net 2.0.</p>
<p><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:small;color:#000000;font-family:Georgia;">Required Namespace:</span><br />
using</span><span style="font-size:10pt;font-family:'Courier New';"> System.DirectoryServices;</span> </p>
<p class="MsoNormal" style="margin:0;"><span style="font-size:10pt;font-family:'Courier New';"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:small;color:#000000;font-family:Georgia;">Authenticate Method:</span></span></span></p>
<p><span style="font-size:x-small;color:#0000ff;"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:x-small;color:#0000ff;">public</span><span style="font-size:x-small;color:#000000;"> </span><span style="font-size:x-small;color:#0000ff;">bool</span><span style="font-size:x-small;color:#000000;"> Authenticate(</span><span style="font-size:x-small;color:#0000ff;">string</span><span style="font-size:x-small;color:#000000;"> userName, </span><span style="font-size:x-small;color:#0000ff;">string</span><span style="font-size:x-small;"><span style="color:#000000;"> password)<br />
</span>{<br />
</span><span style="font-size:x-small;color:#2b91af;">  DirectoryEntry</span><span style="font-size:x-small;"> deSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#0000ff;">  object</span><span style="font-size:x-small;"> obj = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#2b91af;">  DirectorySearcher</span><span style="font-size:x-small;"> dsSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#2b91af;">  SearchResult</span><span style="font-size:x-small;"> srSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;</span><span style="font-size:x-small;">  bool isAuthenticated = <span style="font-size:x-small;color:#0000ff;">false</span><span style="font-size:x-small;">;</span></span></span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:x-small;"><span style="font-size:x-small;color:#0000ff;">  try<br />
</span><span style="font-size:x-small;">    {</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#008000;">    // Initialize the Directory Entry with LDAP Connection String and use Domain, User Name &amp; Password to Authenticate<br />
</span><span style="font-size:x-small;">    deSystem = </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">DirectoryEntry</span><span style="font-size:x-small;">(</span><span style="font-size:x-small;color:#a31515;">&#8220;LDAP://mydomain.com/CN=MyGroup,CN=Users,DC=mydomain,DC=com&#8221;</span><span style="font-size:x-small;">, </span><span style="font-size:x-small;color:#a31515;">&#8220;DOMAIN\\&#8221;</span><span style="font-size:x-small;"> + userName, password);</span></span></span></span></span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:x-small;"><span style="font-size:x-small;color:#008000;">    // Bind to the native AdsObject to force authentication<br />
</span><span style="font-size:x-small;">    obj = deSystem.NativeObject;</span><span style="font-size:x-small;">    dsSystem = <span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">DirectorySearcher</span><span style="font-size:x-small;">(deSystem);</span></span><span style="font-size:x-small;"><span style="font-size:x-small;color:#0000ff;">    if</span><span style="font-size:x-small;"> (dsSystem != </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">)<br />
    {<br />
</span><span style="font-size:x-small;color:#008000;">      // Search subtree of UserDN<br />
</span><span style="font-size:x-small;">      dsSystem.SearchScope = </span><span style="font-size:x-small;color:#2b91af;">SearchScope</span><span style="font-size:x-small;">.Subtree;</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#008000;">      // Find the user data<br />
</span><span style="font-size:x-small;">      srSystem = dsSystem.FindOne();</span></span></span></span></span></span></p>
<div><span style="font-size:x-small;color:#0000ff;"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:x-small;color:#0000ff;">      if</span><span style="font-size:x-small;"> (srSystem == </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">)<br />
      {<br />
        deSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
        dsSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#0000ff;">        throw</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">Exception</span><span style="font-size:x-small;">(</span><span style="font-size:x-small;color:#a31515;">&#8220;&#8216;UserName&#8217; is not authorized to access the Active Directory. Access Denied!&#8221;</span><span style="font-size:x-small;">);<br />
      }<br />
</span><span style="font-size:x-small;">    }<br />
    </span><span style="font-size:x-small;color:#0000ff;">else<br />
    </span><span style="font-size:x-small;">{<br />
      deSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
      dsSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;</span><span style="font-size:x-small;">      <span style="font-size:x-small;color:#0000ff;">throw</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">Exception</span><span style="font-size:x-small;">(</span><span style="font-size:x-small;color:#a31515;">&#8220;Invalid User Name or Password. Access Denied!&#8221;</span><span style="font-size:x-small;">);<br />
    }</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#008000;">    // Pick up the user group belong to<br />
</span><span style="font-size:x-small;color:#008000;">    // Determine wheter the User is Member of Users, Domain Users or Administrators Group<br />
</span><span style="font-size:x-small;color:#2b91af;">    ResultPropertyValueCollection</span><span style="font-size:x-small;"> propValColl = srSystem.Properties[</span><span style="font-size:x-small;color:#a31515;">"member"</span><span style="font-size:x-small;">];</span></span></span><span style="font-size:x-small;"><span style="font-size:x-small;color:#0000ff;">    if</span><span style="font-size:x-small;"> (propValColl.Count &gt; 0)<br />
    {<br />
</span><span style="font-size:x-small;color:#0000ff;">      foreach</span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#0000ff;">object</span><span style="font-size:x-small;"> propVal </span><span style="font-size:x-small;color:#0000ff;">in</span><span style="font-size:x-small;"> propValColl)<br />
      {<br />
</span><span style="font-size:x-small;color:#008000;">        // Check user exist in Group we are searching for<br />
</span><span style="font-size:x-small;color:#0000ff;">        string</span><span style="font-size:x-small;">[] strDN = deSystem.Path.Split(</span><span style="font-size:x-small;color:#a31515;">&#8220;/&#8221;</span><span style="font-size:x-small;">.ToCharArray());</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#0000ff;">        string</span><span style="font-size:x-small;"> tmpPath = strDN[0] + </span><span style="font-size:x-small;color:#a31515;">&#8220;//&#8221;</span><span style="font-size:x-small;"> + strDN[2] + </span><span style="font-size:x-small;color:#a31515;">&#8220;/&#8221;</span><span style="font-size:x-small;"> + propVal.ToString();</span></span></span></span></span></div>
<div><span style="font-size:x-small;color:#0000ff;"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:x-small;"> </span></span></span></div>
<div><span style="font-size:x-small;color:#0000ff;"><span style="font-size:10pt;color:#0000ff;font-family:'Courier New';"><span style="font-size:x-small;"><br />
</span></span></span></div>
<div><span style="font-size:x-small;"><span style="font-size:x-small;color:#2b91af;">        DirectoryEntry</span><span style="font-size:x-small;"> tmpDirEntry = </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">DirectoryEntry</span><span style="font-size:x-small;">(tmpPath, ADConnection.GetDomain() + </span><span style="font-size:x-small;color:#a31515;">&#8220;\\&#8221;</span><span style="font-size:x-small;"> + userName, password, </span><span style="font-size:x-small;color:#2b91af;">AuthenticationTypes</span><span style="font-size:x-small;">.Secure);</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#2b91af;">        DirectorySearcher</span><span style="font-size:x-small;"> tmpDirSearcher = </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">DirectorySearcher</span><span style="font-size:x-small;">(tmpDirEntry);</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#2b91af;">        SearchResult</span><span style="font-size:x-small;"> tmpDirSR = tmpDirSearcher.FindOne();</span></span></span><span style="font-size:x-small;"><span style="font-size:x-small;color:#0000ff;">        if</span><span style="font-size:x-small;"> (tmpDirSR == </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">)<br />
</span><span style="font-size:x-small;color:#0000ff;">          continue</span><span style="font-size:x-small;">;</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#2b91af;">        ResultPropertyValueCollection</span><span style="font-size:x-small;"> tmpPropValColl = tmpDirSR.Properties[</span><span style="font-size:x-small;color:#a31515;">"samaccountname"</span><span style="font-size:x-small;">];</span></span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">        if</span><span style="font-size:x-small;"> (tmpPropValColl.Count &gt; 0)<br />
        {<br />
</span><span style="font-size:x-small;color:#0000ff;">          foreach</span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#0000ff;">object</span><span style="font-size:x-small;"> tmpPropVal </span><span style="font-size:x-small;color:#0000ff;">in</span><span style="font-size:x-small;"> tmpPropValColl)<br />
          {<br />
</span><span style="font-size:x-small;color:#0000ff;">            if</span><span style="font-size:x-small;"> (tmpPropVal.ToString().ToLower() == userName.ToLower())<br />
            {<br />
</span><span style="font-size:x-small;color:#2b91af;">              ResultPropertyValueCollection</span><span style="font-size:x-small;"> tmpPropNames = tmpDirSR.Properties[</span><span style="font-size:x-small;color:#a31515;">"name"</span><span style="font-size:x-small;">];<br />
</span><span style="font-size:x-small;color:#0000ff;">              foreach</span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#0000ff;">object</span><span style="font-size:x-small;"> tmpPropName </span><span style="font-size:x-small;color:#0000ff;">in</span><span style="font-size:x-small;"> tmpPropNames)<br />
</span><span style="font-size:x-small;color:#0000ff;">                base</span><span style="font-size:x-small;">.Session[</span><span style="font-size:x-small;color:#a31515;">"CurrentUserName"</span><span style="font-size:x-small;">] = tmpPropName.ToString();<br />
</span><span style="font-size:x-small;color:#0000ff;">              base</span><span style="font-size:x-small;">.Session[</span><span style="font-size:x-small;color:#a31515;">"CurrentDomain"</span><span style="font-size:x-small;">] = </span><span style="font-size:x-small;color:#a31515;">&#8220;DOMAIN&#8221;</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#0000ff;">              base</span><span style="font-size:x-small;">.Session[</span><span style="font-size:x-small;color:#a31515;">"CurrentUserID"</span><span style="font-size:x-small;">] = userName;</span><span style="font-size:x-small;">              isAuthenticated = <span style="font-size:x-small;color:#0000ff;">true</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;color:#0000ff;">              break</span><span style="font-size:x-small;">;<br />
            }<br />
          }<br />
        }<br />
</span><span style="font-size:x-small;color:#0000ff;">        else<br />
          </span><span style="font-size:x-small;color:#0000ff;">continue</span><span style="font-size:x-small;">;</span><span style="font-size:x-small;"><span style="font-size:x-small;color:#0000ff;">        if</span><span style="font-size:x-small;"> (isAuthenticated)<br />
          </span><span style="font-size:x-small;color:#0000ff;">break</span><span style="font-size:x-small;">;<br />
      }<br />
    }<br />
    propValColl = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
</span><span style="font-size:x-small;">  }<br />
</span><span style="font-size:x-small;color:#0000ff;">  catch</span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#2b91af;">DirectoryServicesCOMException</span><span style="font-size:x-small;"> dsEx)<br />
  {<br />
    </span><span style="font-size:x-small;color:#0000ff;">throw</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">Exception</span><span style="font-size:x-small;">(dsEx.Message);<br />
  }<br />
</span><span style="font-size:x-small;color:#0000ff;">  catch</span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#2b91af;">Exception</span><span style="font-size:x-small;"> ex)<br />
  {<br />
    </span><span style="font-size:x-small;color:#0000ff;">throw</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#0000ff;">new</span><span style="font-size:x-small;"> </span><span style="font-size:x-small;color:#2b91af;">Exception</span><span style="font-size:x-small;">(ex.Message);<br />
  }<br />
</span><span style="font-size:x-small;color:#0000ff;">  finally<br />
</span><span style="font-size:x-small;">  {<br />
    deSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
    obj = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
    adSecurity = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
    dsSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
    srSystem = </span><span style="font-size:x-small;color:#0000ff;">null</span><span style="font-size:x-small;">;<br />
  }<br />
</span><span style="font-size:x-small;color:#0000ff;">  return</span><span style="font-size:x-small;"> isAuthenticated;<br />
}</span></span></span></span></span></div>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/shiervermont.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/shiervermont.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/shiervermont.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/shiervermont.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/shiervermont.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/shiervermont.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/shiervermont.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/shiervermont.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/shiervermont.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/shiervermont.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/shiervermont.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/shiervermont.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=shiervermont.wordpress.com&blog=1200712&post=3&subd=shiervermont&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://shiervermont.wordpress.com/2008/03/19/authenticating-and-retrieving-users-full-name-via-ldap-using-aspnet-20/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/da6f5515930d4c025fe2486a2bc64511?s=96&#38;d=identicon" medium="image">
			<media:title type="html">shiervermont</media:title>
		</media:content>
	</item>
	</channel>
</rss>