<?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>Monie Studios &#187; microsoft</title>
	<atom:link href="http://www.moniestudios.com/tag/microsoft/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.moniestudios.com</link>
	<description>So much to learn, so little time!</description>
	<lastBuildDate>Fri, 21 Oct 2011 11:30:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Using Microsoft Active Directory Authentication</title>
		<link>http://www.moniestudios.com/tutorials/active-directory-authentication/</link>
		<comments>http://www.moniestudios.com/tutorials/active-directory-authentication/#comments</comments>
		<pubDate>Tue, 19 May 2009 00:08:44 +0000</pubDate>
		<dc:creator>Monie</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[ad]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[server]]></category>

		<guid isPermaLink="false">http://www.moniestudios.com/?p=374</guid>
		<description><![CDATA[In the previous tutorial, we've talked about <a href="http://www.moniestudios.com/tutorials/ad-report/">How to Get Active Directory Accounts Information Using VB Script</a>. In this tutorial, we will learn how you can make use of your AD information to create a login page for your local system.]]></description>
			<content:encoded><![CDATA[<p>Microsoft Active Directory provides a directory service that allows organizations to administer their networked resources. One of the goals of a directory service is to maintain a common database of all information needed for controlling user access, computers, printers, and other network resources.</p>
<p>In this tutorial, I will show you how you can develop an AD Authentication page for your intranet web based application.</p>
<h2>The Benefits</h2>
<p style="clear: both;">When you are developing an intranet web based application system, that will only be access or used within your company or organization, it is best to use the existing AD as your authentication method.</p>
<p>Not only it saves you time and space as you don&#8217;t have to create a specific authentication page and database to store the user, the AD itself is very hard to hack through.</p>
<h2>Step 1</h2>
<p>Like any other system, validation is very crucial. First, you should create a basic HTML form where you can typed in your username, password and domain name. This code will actually gather all the value from your html form and submit the value into the authentication function as parameter (code line #13).</p>
<p>Of course, if the user exist the function will then redirect the user to a success page and redirect the user to an error page if the user is invalid.</p>
<pre class="brush: vb; title: ; notranslate">
'// 1. Form Validation
Dim Submit, UserName, Password, Domain, Result, Message
Submit = Request.Form(&quot;Submit&quot;)

If Submit = &quot;Authenticate&quot; Then

    'Get the input from your HTML form
    UserName = Request.Form(&quot;UserName&quot;)
    Password = Request.Form(&quot;Password&quot;)
    Domain = Request.Form(&quot;Domain&quot;)

    'Call the AuthenticateUser() function to do the verification process
    Result = AuthenticateUser(UserName, Password, Domain)

    If Result Then
        'If user exist, then redirect to success page
        Response.Redirect (&quot;success.asp&quot;)
    Else
        'If user don't exist, redirect to error page
        Response.Redirect (&quot;error.asp&quot;)
    End If
End If
</pre>
<h2>Step 2</h2>
<p>Once the information has been submitted to the Authentication() function, this is where the verification process begin!</p>
<p>This code will actually check whether the entered detail (Username, Password and Domain) matched with any data inside the AD Database. If you are familiar with sql language, then you will know what is happening in code line #10 onwards, maybe the sql query statement is a bit different, but the idea and method are the same.</p>
<pre class="brush: vb; title: ; notranslate">
'// 2. Authenticate Function
Function AuthenticateUser(UserName, Password, Domain)
    Dim strUser, strPassword, strQuery, oConn, cmd, oRS

    'Assume Failure
    AuthenticateUser = false
    strUser = UserName
    strPassword = Password

    strQuery = &quot;SELECT cn FROM 'LDAP://&quot; &amp; Domain &amp; &quot;' WHERE objectClass='*'&quot;
    Set oConn = server.CreateObject(&quot;ADODB.Connection&quot;)
        oConn.Provider = &quot;ADsDSOOBJECT&quot;
        oConn.Properties(&quot;User ID&quot;) = strUser
        oConn.Properties(&quot;Password&quot;) = strPassword
        oConn.Properties(&quot;Encrypt Password&quot;) = true
        oConn.open &quot;DS Query&quot;, strUser, strPassword

    Set cmd = server.CreateObject(&quot;ADODB.Command&quot;)
    Set cmd.ActiveConnection = oConn
    cmd.CommandText = strQuery

    On Error Resume Next
    Set oRS = cmd.Execute

    If oRS.bof OR oRS.eof Then
        AuthenticateUser = False
    Else
        AuthenticateUser = True
    End if

    Set oRS = Nothing
    Set oConn = nothing
End Function
</pre>
<h2>Final Step</h2>
<p>Putting it all together, your login page will be much more like this.</p>
<p>This is a very basic ASP Form and is only for tutorial purposes. If you were to use this code on your web site, you would want to do some extra form validation to make sure the compulsory field are filled in, checking for empty field, etc.</p>
<pre class="brush: vb; title: ; notranslate">
&lt;% @LANGUAGE=&quot;VBSCRIPT&quot; EnableSessionState = False %&gt;
&lt;%
Option Explicit
Response.Buffer = True  

'// 1. Form Validation
Dim Submit, UserName, Password, Domain, Result, Message
Submit = Request.Form(&quot;Submit&quot;)

If Submit = &quot;Authenticate&quot; Then

    'Get the input from your HTML form
    UserName = Request.Form(&quot;UserName&quot;)
    Password = Request.Form(&quot;Password&quot;)
    Domain = Request.Form(&quot;Domain&quot;)

    'Call the AuthenticateUser() function to do the verification process
    Result = AuthenticateUser(UserName, Password, Domain)

    If Result Then
        'If user exist, then redirect to success page
        Response.Redirect (&quot;success.asp&quot;)
    Else
        'If user don't exist, redirect to error page
        Response.Redirect (&quot;error.asp&quot;)
    End If
End If

'// 2. Authenticate Function
Function AuthenticateUser(UserName, Password, Domain)
    Dim strUser, strPassword, strQuery, oConn, cmd, oRS

    'Assume Failure
    AuthenticateUser = false
    strUser = UserName
    strPassword = Password

    strQuery = &quot;SELECT cn FROM 'LDAP://&quot; &amp; Domain &amp; &quot;' WHERE objectClass='*'&quot;
    Set oConn = server.CreateObject(&quot;ADODB.Connection&quot;)
        oConn.Provider = &quot;ADsDSOOBJECT&quot;
        oConn.Properties(&quot;User ID&quot;) = strUser
        oConn.Properties(&quot;Password&quot;) = strPassword
        oConn.Properties(&quot;Encrypt Password&quot;) = true
        oConn.open &quot;DS Query&quot;, strUser, strPassword

    Set cmd = server.CreateObject(&quot;ADODB.Command&quot;)
    Set cmd.ActiveConnection = oConn
    cmd.CommandText = strQuery

    On Error Resume Next
    Set oRS = cmd.Execute

    If oRS.bof OR oRS.eof Then
        AuthenticateUser = False
    Else
        AuthenticateUser = True
    End if

    Set oRS = Nothing
    Set oConn = nothing
End Function
%&gt;

&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Using Microsoft Active Directory Authentication&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form name=&quot;DomainAuthentication&quot; method=&quot;post&quot;&gt;
    Username:&lt;input type=&quot;text&quot; name=&quot;UserName&quot; size=&quot;45&quot;&gt;
    Password:&lt;input type=&quot;password&quot; name=&quot;Password&quot; size=&quot;45&quot;&gt;
    AD Domain:&lt;input type=&quot;text&quot; name=&quot;Domain&quot; size=&quot;45&quot;&gt;
    &lt;input name=&quot;submit&quot; type=&quot;submit&quot; value=&quot;Authenticate&quot;&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><a class="mw" href="http://www.moniestudios.com/wp-content/uploads/attachment/authentication.zip">Download This Code | Total Download: [download#1#hits]</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.moniestudios.com/tutorials/active-directory-authentication/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

