VB icon

Complete working Basic Authentication

Email
Submitted on: 1/5/2015 9:00:00 AM
By: Almar Joling (from psc cd)  
Level: Beginner
User Rating: By 28 Users
Compatibility: ASP (Active Server Pages)
Views: 2254
 
     This code shows your visitors the Basic Authentication dialog (or NT Login Dialog) It also returns the password and the username If you like it, please vote for this 16 year old programmer :o)

 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
'**************************************
' for :Complete working Basic Authentication
'**************************************
The Base64 decryption algorithm I used came from http://www.aspcode.net. Showing the dialog is completely by me
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Complete working Basic Authentication
' Description:This code shows your visitors the Basic Authentication dialog (or NT Login Dialog)
It also returns the password and the username
If you like it, please vote for this 16 year old programmer :o)
' By: Almar Joling (from psc cd)
'
' Inputs:In the dialog the username and password (/and domain)
'
' Returns:The password and username given by the visitors of your site
'
' Assumes:Paste it and run it. It does not verify any usernames or so.
'
' Side Effects:Protects your site :o))
'**************************************

<%
Response.Buffer = True
Response.Clear
Dim Myname, MyPass
GetUser Myname, MyPass
Response.Write MyName & "->" & MyPass
if len(Myname) = 0 then
 Response.Status = "401 Unauthorized"
 Response.AddHeader "WWW-Authenticate","BASIC Realm=enter your realm here"
 Response.End
End If
Sub GetUser(LOGON_USER, LOGON_PASSWORD)
 Dim UP, Pos, Auth
 Auth = Request.ServerVariables("HTTP_AUTHORIZATION")
 LOGON_USER = ""
 LOGON_PASSWORD = ""
 If LCase(Left(Auth, 5)) = "basic" Then
UP = Base64Decode(Mid(Auth, 7))
Pos = InStr(UP, ":")
If Pos > 1 Then
 LOGON_USER = Left(UP, Pos - 1)
 LOGON_PASSWORD = Mid(UP, Pos + 1)
End If
 End If
End Sub
' Decodes a base-64 encoded string.
Function Base64Decode(base64String)
 Const Base64CodeBase = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 Dim dataLength, Out, groupBegin
 dataLength = Len(base64String)
 Out = ""
 If dataLength Mod 4 <> 0 Then
Err.Raise 1, "Base64Decode", "Bad Base64 string."
Exit Function
 End If
 ' Now decode each group:
 For groupBegin = 1 To dataLength Step 4
Dim numDataBytes, CharCounter, thisChar, thisData, groupData
' Each data group encodes up To 3 actual bytes.
numDataBytes = 3
groupData = 0
For CharCounter = 0 To 3
 ' <B>Convert</B> each character into 6 bits of data, And add it To
 ' an integer For temporary storage. If a character is a '=', there
 ' is one fewer data byte. (There can only be a maximum of 2 '=' In
 ' the whole string.)
 thisChar = Mid(base64String, groupBegin + CharCounter, 1)
 If thisChar = "=" Then
numDataBytes = numDataBytes - 1
thisData = 0
 Else
thisData = InStr(Base64CodeBase, thisChar) - 1
 End If
 If thisData=-1 Then
Err.Raise 2, "Base64Decode", "Bad character In Base64 string."
Exit Function
 End If
 groupData = 64 * groupData + thisData
Next
' Convert 3-byte integer into up To 3 characters
Dim OneChar
For CharCounter = 1 To numDataBytes
 Select Case CharCounter
Case 1: OneChar = groupData \ 65536
Case 2: OneChar = (groupData And 65535) \ 256
Case 3: OneChar = (groupData And 255)
 End Select
 Out = Out & Chr(OneChar)
Next
 Next
 Base64Decode = Out
End Function
%>


Other 1 submission(s) by this author

 


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 Beginner 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.