Click here to Skip to main content
15,887,485 members
Articles / Web Development / IIS
Article

Web-based Active Directory Login

Rate me:
Please Sign up or sign in to vote.
4.79/5 (18 votes)
9 Oct 20032 min read 418.6K   9.2K   59   99
Web-based Active Directory Login

Image 1

Overview

Web-based Active Directory Login implements central sign-on system for web-based applications. It was developed to eliminate maintenance of user passwords in database, whether encrypted or not. A user running application from desktop enjoys liberty to access resources and/or services on the network which he has permission to do from Windows. On the other hand, web-based applications run in a security context entirely different from that of a desktop application. Same user while running application from browser will not have such liberty. This was the problem when I tried to login using my n/w user name and password maintained in Active Directory through web. Ultimately it was overcome by impersonating the web server anonymous user which in most cases is IUSR_machinename.

Using the code

Below is a brief description of how to use the code.

There are two class files

  1. LoginAdmin
  2. prjLogin

LoginAdmin is an ActiveX DLL type project and contains a standard module and a class module. The ImpersonateUser class has two methods which you will be using in your ASP code.

VBScript
' create an object of ImpersonateUser class
 Set objLogon = Server.CreateObject("LoginAdmin.ImpersonateUser")
 
' any domain user who has rights to access active directory
 objLogon.Logon "user id", "password", "domain name"

 objLogon.Logoff
 Set objLogon = Nothing

These are the methods of the class ImpersonateUser and their description:

MethodDescription
Logon(strUser, strPassword, strDomain)This method should be called before sending request to active directory. The user should be a valid domain user with at least read permissions of active directory. You can keep this user in a database or hardcode it's userid and password in the ASP script.
LogOff()This method must be called after accessing info from Active Directory in order for IIS to revert security permissions of the particular file

prjLogin is also an ActiveX DLL type project and contains only a class module. It uses references to Active DS type library. The clsDomainLogin class has one method with three parameters user name, password and domain.

VBScript
' create an object of clsDomainLogin class
 Set oUser = Server.CreateObject("prjLogin.clsDomainLogin")

' BindObject has three parameters userid, password and domain name
 iResult = oUser.BindObject(strUser, strPassword, strDomain)

Below is a method of the class clsDomainLogin and its description:

MethodDescription
BindObject(strUser, strPassword, strDomain)This method should be called while authenticating from Active Directory. It returns 1 when successful and 0 when unsuccessful

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 The Shams Group
Pakistan Pakistan
Faisal is Senior Team Lead at The Shams Group, Karachi. He has 10+ years of experience in the field of software/ web development and infrastructure management.
He has worked on various tools and platform which includes VB, ASP, ASP.NET, Javascript, COM/COM+, SQL Server, Informix, Active Directory, UNIX/Solaris, WSH, Windows NT/ 2000 servers, RADIUS, SMTP, POP, IMAP etc.
He likes to watch movies, hangs out with friends and loves to make new friends.

Comments and Discussions

 
AnswerRe: problem Pin
Faisal Haroon30-Apr-06 10:55
Faisal Haroon30-Apr-06 10:55 
GeneralI got an error, please help me Pin
t20amarin19-Apr-06 20:53
t20amarin19-Apr-06 20:53 
GeneralRe: I got an error, please help me Pin
Faisal Haroon30-Apr-06 11:35
Faisal Haroon30-Apr-06 11:35 
GeneralLogin Pin
c1ph37-Mar-06 9:42
c1ph37-Mar-06 9:42 
GeneralRe: Login Pin
Faisal Haroon2-May-06 6:07
Faisal Haroon2-May-06 6:07 
GeneralLogin Fix and Side Question Pin
mmarx8227-Feb-06 7:24
mmarx8227-Feb-06 7:24 
GeneralRe: Login Fix and Side Question Pin
mmora57188-Sep-10 10:31
mmora57188-Sep-10 10:31 
GeneralRe: Login Fix and Side Question Pin
mmarx8210-Sep-10 3:22
mmarx8210-Sep-10 3:22 
I use this page to authenticate multiple websites and as a backup location so you will need to modify as needed, if your having problems logging in make sure you registered the projLogin.dll on your server.

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
   ' Form was autopostback, grab the form variables ...
		on error resume next
		
		  strUser = Request("oUser")


		  UserArray = Split(strUser, "@")
		  strUser = UserArray(0)


		  strPassword = Request("oPassword")
		  
		  returnpage = request.form("returnpage")
		
	'Allows the user mstewart to bypass the System Maintanence when active but still authenticates to active directory.  
	if strUser <> "mstewart" then
	
		if request.QueryString("Service") = "True" then
			response.Redirect(returnpage & "?Service=True")
		end if
		
	end if
	
     Dim objLogon

     ' create an object for impersonating IIS to use a valid domain user instead of anonymous user IUSR_machinename
     Set objLogon = Server.CreateObject("LoginAdmin.ImpersonateUser")

     ' any domain user who has rights to access active directory
     objLogon.Logon "user id", "password", "domain name"

    ' check here for a form post ...
    if request("GETUSER") = "" Then
%>
		
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml">
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Backup Login Place</title>
		</head>
		<style>
		#main {
		font:Arial, Helvetica, sans-serif;
		color:#666666;
		}
	
		</style>
		<body onLoad="document.Logon.oUser.focus()">
        <script type="text/javascript">
        	document.write("<table style='width:100%; height:"+ (screen.height - 200) +"px; background-color:#F8F8F8; border:#000000; border-style:solid; border-width:thin;'>")
        </script>
        
        <tr valign="top">
            <td style="font:Arial, Helvetica, sans-serif; color:#336699; padding-left:15px; padding-top:15px"><strong>Backup Login Place</strong></td>
        </tr>
			<tr>
				<td>
					<table width="100%" id="main" class="main">
                        <tr valign="middle">
                        	<td colspan="2" align="center">
                                <table width="100%">
                                <% if request.QueryString("Submitted") = "True" then %>
                                	
                                    <tr>
                                        <td colspan="2">Thank You <strong><%=request.QueryString("FName")%>&nbsp;<%=request.QueryString("LName")%></strong>, your information has been submitted.</td>                                    </tr>
                                    <tr>
                                        <td colspan="2">&nbsp;</td>
                                    </tr>
                                                                        
                                <% end if %>
                                    <tr>
                                        <td colspan="2" align="center">Login Below</td>
                                    </tr>
                                    <tr>
                                        <td colspan="2">&nbsp;</td>
                                    </tr>
                                    <tr>
                                    	<td colspan="2">
                                        	<table width="100%">
                                                <FORM Name="Logon" ACTION="" METHOD="POST">
                                                <input type="hidden" name="studentaccess" id="studentaccess" value="Denied" />
                                                <tr valign="middle">
                                                    <td width="45%" align="right">Username:</td>
                                                    <td width="55%" align="left"><input type="text" name="oUser" size="20" maxlength="100" /></td>
                                                </tr>
                                                <tr valign="middle">
                                                    <td width="45%" align="right">Password:</td>
                                                    <td width="55%" align="left"><input type="password" name="oPassword" size="22" maxlength="100" /></td>
                                                </tr>
                                                <tr valign="middle">
                                                    <td width="45%" align="right">URL to Login:</td>
                                                    <td width="55%" align="left"><input type="text" name="returnpage" id="returnpage" size="50" maxlength="100" value="http://intwebserv01/Hit System/"/></td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2">&nbsp;</td>
                                                </tr>
                                                <tr>
                                                    <td colspan="2" align="center"><input type="submit" name="GETUSER" value="Login" /></td>
                                                </tr>
                                                </FORM>
                                            </table>
                                        </td>
                                    </tr>
                                </table>
							</td>
						</tr>
					</table>
				</td>
			</tr>
			
		</table>
		</body>
		</html>

<%

	else
	
		  Set oUser = Server.CreateObject("prjLogin.clsDomainLogin")

			strDomain = "MICROSOFT.COM"

		  ' BindObject has two parameters userid and password
		  iResult = oUser.BindObject(strUser, strPassword, strDomain)


'on error resume next
' Set reference to the ADSI interface to NT User Manager ...

	' Checks for blank username.
	if strUser = "" then
		iResult = "0"
	end if

		'Setup Connection to Database
		on error resume next
		Dim objConfig  ' As CDO.Configuration
		Dim objMessage ' As CDO.Message
		Dim Fields     ' As ADODB.Fields
		
		Set adoCon = Server.CreateObject("ADODB.Connection")
		adoCon.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("DataBases\PreLogin.mdb")
		Set rs = Server.CreateObject("ADODB.Recordset")
	
	'The user passed authentication
	if iResult = 1 then
	
		Set User = GetObject("WinNT://" & strDomain & "/" & strUser & ",user")
	
			' Test for Student Logins then Deny's Access
			if request.Form("studentaccess") = "Denied" or request.Form("studentaccess") <> "Allowed" then
			
				' First Test - Student logins use numbers as usernames so we test to see if the username is a number.
				if IsNumeric(strUser) then
					response.Redirect(returnpage & "?Error=Student")
				end if
	
	
				' Second test for Student Logins			
				strSQL = "SELECT * FROM DenyLogin WHERE DenyLogin LIKE '%" & strUser & "%' ;"
				rs.LockType = 3
				rs.Open strSQL, adoCon
				do while not rs.EOF	
				
				if rs.fields("DenyLogin") = strUser then
				
					response.Redirect(returnpage & "?Error=Student")
				
				end if
				rs.movenext
				loop
				rs.close
				
			end if

		'You have passed validation. Since this is the backup login place redirect to the site that called it.
			response.Redirect(returnpage & "Login.asp?UserName=" & strUser & "&Name=" & User.Fullname & "&Password=" & strPassword & "&GETUSER=Login")
	
	'The user failed authentication
	else
	
			response.Redirect(returnpage & "?Error=Credentials")
	
	end if

	set oUser = Nothing

	adoCon.close

end if

objLogon.Logoff
Set objLogon = Nothing

%>


Marcus Stewart

GeneralRe: Login Fix and Side Question Pin
mmora571810-Sep-10 10:13
mmora571810-Sep-10 10:13 
GeneralQuetion about : Web-based Active Directory Login Pin
Sidhi_OK3-Feb-06 22:03
Sidhi_OK3-Feb-06 22:03 
GeneralQuestion Pin
kickone16-Oct-05 15:18
kickone16-Oct-05 15:18 
GeneralLogin problem Pin
Member 227742515-Sep-05 17:28
Member 227742515-Sep-05 17:28 
GeneralLogoff Pin
Healeyboy29-Jun-05 5:05
Healeyboy29-Jun-05 5:05 
GeneralRe: Logoff Pin
Faisal Haroon7-Jul-05 0:14
Faisal Haroon7-Jul-05 0:14 
General100 Percent CPU Util Pin
DerDeutscher29-May-05 17:24
DerDeutscher29-May-05 17:24 
GeneralRe: 100 Percent CPU Util Pin
Faisal Haroon7-Jul-05 0:33
Faisal Haroon7-Jul-05 0:33 
GeneralLogin Issue Pin
Anonymous28-Mar-05 12:58
Anonymous28-Mar-05 12:58 
GeneralRe: Login Issue Pin
Faisal Haroon13-Jul-05 8:07
Faisal Haroon13-Jul-05 8:07 
GeneralI got an error Pin
goodguytop25-Feb-05 14:24
goodguytop25-Feb-05 14:24 
GeneralRe: I got an error Pin
Faisal Haroon25-Feb-05 20:26
Faisal Haroon25-Feb-05 20:26 
QuestionLogin Success!!!????? Pin
S.M.Khurram Quaseem13-Jan-05 0:31
S.M.Khurram Quaseem13-Jan-05 0:31 
AnswerRe: Login Success!!!????? Pin
Faisal Haroon13-Jan-05 3:11
Faisal Haroon13-Jan-05 3:11 
GeneralRe: Login Success!!!????? Pin
S.M.Khurram Quaseem13-Jan-05 13:40
S.M.Khurram Quaseem13-Jan-05 13:40 
GeneralRe: Login Success!!!????? Pin
Faisal Haroon14-Jan-05 2:26
Faisal Haroon14-Jan-05 2:26 
GeneralERROR IN PAGE, PLS HELP Pin
thamir1002-Jan-05 19:57
thamir1002-Jan-05 19:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.