VB icon

Authenticate against Windows NT/2000 Domain

Email
Submitted on: 1/6/2015 10:14:00 AM
By: MarkParter (from psc cd)  
Level: Intermediate
User Rating: By 12 Users
Compatibility: ASP (Active Server Pages)
Views: 1735
 
     This code will take the users username and password from a form and use them to authenticate them against a Windows NT/2000 domain. Unlike other examples, you do not need to switch on 'Basic' or 'Integrated Windows' permissions for the webite on IIS. You can leave the setting as Anonymous Access.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Authenticate against Windows NT/2000 Domain
' Description:This code will take the users username and password from a form and use them to authenticate them against a Windows NT/2000 domain. Unlike other examples, you do not need to switch on 'Basic' or 'Integrated Windows' permissions for the webite on IIS. You can leave the setting as Anonymous Access.
' By: MarkParter (from psc cd)
'
' Inputs:Form inputs
'
' Returns:Whether or not the users has been authenticated
'
' Assumes:Make sure that the site being protected is set to Anonymous Authentication and that the users using the site all have Windows accounts.
'
' Side Effects:This is highly insecure over the Internet. I recommend you use SSL to protect user details.
Also, only protects ASP pages.
'**************************************

'Place the following in your ASP page handling the server-side authetication.
'************************************************
<% Dim objADSI, strUsername, strPassword, strDomain
strUsername = Trim(Request.Form("txtUsername"))
strPassword = Trim(Request.Form("txtPassword"))
strDomain = "Intranet"
'you can easily change this to retrieve the domain from a form aswell
Set objADSI = GetObject("WinNT://" & strDomain)
 
Dim strADsNamespace
Dim objADSINamespace
strADsNamespace = Left("WinNT://" & strDomain, InStr("WinNT://" & strDomain, ":"))
Set objADSINamespace = GetObject(strADsNamespace)
Set objADSI = objADSINamespace.OpenDSObject("WinNT://" & strDomain, strDomain & "\" & strUsername, strPassword, 0)
' If there's no error then the user has been authenticated!
If Err.Number <> 0 Then 'authentication failed
'code here for failed authentication
Session("authenticated") = False
Else
'code here for authentication success
Session("authenticated") = True
End If
Set objADSINamespace = Nothing
Set objADSI = Nothing
Set strUsername = Nothing
Set strPassword = Nothing
Set strDomain = Nothing
Set strADsNamespace = Nothing %>
'***********************************************
At the top of all your protected ASP pages place the following:
<!-- #INCLUDE file="check.asp" -->
Make sure you check the path to the file, if necessary make it an absolute include, i.e. <!-- #INCLUDE file="http://www.yoursite.co.uk/check.asp" -->
'************************************************
create a file called check.asp, in it place the following code:
<% If Session("authenticated") <> True Then
 Session.Abandon 'clear any session variables
 Response.Redirect "login.asp" 'kick them back to the login page
End If %>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.