This step-by-step guide explains the correct ASP syntax for
retrieving user authentication information from a Web site visitor.
NoteThis information can only be captured if the site uses Basic
Authentication, Windows NT Challenge Response (NTLM), or Windows Integrated
Authentication.
Requirements
- A Windows server that runs Internet Information Services
(IIS) 4.0 or later
- A text or ASP editor such as Notepad or Microsoft Visual
InterDev
The Code
To capture the visitor's domain and login, paste the following
code into your ASP page. If the page also contains HTML, paste this code above
the HTML tag.
<%
Dim strUser
strUser = Request.ServerVariables("LOGON_USER")
Response.Write strUser
%> This code will capture the user's domain and login, and then display it
in the browser.
Making the Code More Useful
Now that you have a method for capturing the user's login
information, you may want to store it somewhere. The steps in this section
demonstrate how to create a text file and write the information to it. Note
that this method is only one of many that use Request.ServerVariables.
- Right-click Start, and then click Explore to open Windows Explorer.
- In Windows Explorer, click File, point to New, and then click Folder.
- This will create a new folder with the name area
highlighted. Type ASP, and then press ENTER to name the
new folder "ASP."
- Paste the following code into your ASP page. If the page
also contains HTML, paste this code above the HTML tag.
<%
Dim objFSO, strUser, objFile
strUser = Request.ServerVariables("LOGON_USER")
set objFSO = Server.CreateObject("scripting.FileSystemObject")
set objFile = objFSO.CreateTextFile("C:\asp\test.txt", true)
objFile.WriteLine strUser
objFile.Close
Response.Write strUser
%>
- This code will write the user's login information to a text
file that is automatically created in the ASP folder (the folder you created in
step 3).