Secure your Web pages with custom authentication
Takeaway: For more control over your authorization protocol, you can set the HTTP status of the response and parse the information as you receive it. In this article, Phillip Perkins explores authentication mechanisms in HTTP.
If you want to secure your Web pages, you would probably set the Web server's security. Then, the Web server will recognize the security restrictions and challenge the requesting client for credentials.
The Web server is actually just sending a 401 response code. It's the client's (i.e., the browser's) responsibility to send a response to the authorization challenge. You can expect browsers to handle this functionality seamlessly. But if you want more control over your authorization protocol, you can set the HTTP status of the response and parse the information as you receive it.
A 401 HTTP response code is a feedback mechanism that tells the client that authentication information is required to view or parse the file in question. The response code is an HTTP header called Status. Once you set the Status code, the browser should take care of the rest. In ASP, this is the code for setting the Status code:
Response.Status = "401 Unauthorized"
You must set the Status before adding any information to the Response buffer. In PHP, you set the Status through the header() function:
header("Status: 401 Unauthorized", true);
Once you challenge the client, you need to send it a method by which it can answer the challenge. The method you should send is the WWW-Authenticate HTTP header, which has four ideal values that you can specify: Basic, Digest, NTLM, and Negotiate.
For simplicity, let's assume that you're only interested in Basic authentication. This value is the easiest to program, and it allows me to show you how to implement this functionality to a further degree.
In the following example, you'll authenticate the client through Basic authentication; however, you'll go one step further and specify that the client can only authenticate on Tuesdays. Here's the ASP code:
<%@ Language=VBScript %>
<%
Option Explicit
Response.Buffer = True
Response.Expires = -1
If Request.ServerVariables("LOGON_USER") = "" Then
Response.Status = "401 Unauthorized"
Response.AddHeader
"WWW-Authenticate","NTLM"
Response.End
Else
If Weekday(Now()) <> 3 Then
Response.Status =
"401 Unauthorized"
Response.AddHeader
"WWW-Authenticate","NTLM"
Response.End
End If
End If
%>
<html>
<head>
<title>Success!</title>
</head>
<body>
You made it!
</body>
</html>
By checking the LOGON_USER HTTP environment variable, you can tell if the user has been authenticated by IIS. If this variable was blank, the user couldn't be authenticated with the given credentials. Finally, check that the day of the week is a Tuesday. If it isn't, set the status to 401 again, and end the response.
To learn more about authentication mechanisms in HTTP, check out the MSDN Web site.
Keep your developer skills sharp by automatically signing up for TechRepublic's free Web Development Zone newsletter, delivered each Tuesday.
SponsoredWhite Papers, Webcasts, and Downloads
- Live Webcast: An Inspection of Modern Malware: How You Can Reduce the Attack Surface Sophos
- Is Real-Time Defragmentation Needed in Today's Environment? Diskeeper
- Enhancing Desktop and Laptop Security Performance with Disk Defragmentation Diskeeper
- Nextel Direct Connect Fact Sheet Sprint
- Live Webcast: Web Threats Don't Discriminate - Large and Small IT Departments Need to be Equally Prepared IronPort Systems
Article Categories
- Security
- Security Solutions, IT Locksmith
- Networking and Communications
- E-mail Administration NetNote, Cisco Routers and Switches
- CIO and IT Management
- Project Management, CIO Issues, Strategies that Scale
- Desktops, Laptops & OS
- Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
- Data Management
- Oracle, SQL Server
- Servers
- Windows NT, Linux NetNote, Windows Server 2003
- Career Development
- Geek Trivia
- Software/Web Development
- Web Development Zone, Visual Basic, .NET





