Tutorials » Web-user Authentication for IIS

First, this tutorial explains Web-user authentication: What it is, how you work with it for Microsoft IIS, and what options are currently available to you.
 
Then second, each choice is described in more detail to help you choose the path that is best for your site.
 
Part 1: What Is Authentication?
Let's assume you want to restrict access to selected portions of your Web site. For example, you might have valuable information, such as real-time stock quotes, or you may desire to charge a monthly fee for accessing your database.
 
In these cases, you will want to let people in, but only after checking that visitors have used an authorized username and password. Additionally, you might want to provide access to the bulk of your Web site for the simple price of a visitor's email address, creating an effective method for tracking visitors.
 
Asking a visitor for their username and password (or their credentials) is called authentication. On the world wide web, the oldest and most widely supported authentication method is Basic Authentication.
 
Definitions What are my choices?
Assuming you have the latest and greatest IIS, you have several choices when working with authentication including: Part 2: What Should I Use?
 
NT challenge response
Using NT Challenge Response is an obvious choice, and is included as one of the options when you set up each IIS directory. Any directory you want to protect must be on an NTFS partition.
 
How to set up NTCR
In Internet Service Manager (IIS1-3) or the Microsoft Management Console for IIS (IIS4 and up) select the directory you want to protect. Make sure Basic (Clear Text) is off and Windows NT Challenge Response is on. You can leave Allow Anonymous on.
 
Create an account for each user you want to provide access, remove the permissions for "IUSR_machinename" from the directory, and add permissions for the added users. Alternatively, you could set up a group, permit access to that group, and add permitted users to the group. Remember, the user will need execute rights if the directory has any ASP, ISAPI extensions, counters, and so on.
 
Note that when the user returns to a non-protected page, they will be prompted for their username and password again, unless you have also granted them read-access to non-protected pages. However cancelling the prompt will let them in, disconcerting though this may be.
 
If the user has permission to access the directory but is in a different domain than the IIS machine, the user will have to prepend the domain name, so IIS knows where to look for the password.
 
Because NTCR uses a token mechanism for verifying users, the password of the currently logged in user is not available to IIS. This will have an impact if you are trying to access a resource which is not on the same machine as IIS, since IIS will not be able to login using the current user to a machine elsewhere on the LAN. For example if an NTCR protected ASP page tried to read an Access mdb file on another machine, it would fail and similarly for an SQL server with integrated or mixed security.
 
See:
Microsoft KB Article Q166029
Microsoft KB Article Q149425.
 
NTFS is the way to go if you are on a windows network.
NTCR can be an ideal solution for intranets, where all users are on accessible domains, there aren't too many users, and you can require the use of a compatible browser (Internet Explorer is the only browser which supports NTCR).
 
You won't want to use NTFS if: IIS Basic Authentication
IIS Basic Authentication is included as an option when you set up each IIS directory. Any directory you want to protect must be on a NTFS partition.
 
How to set up IIS Basic Authentication
Setting up IIS Basic Authentication is similar to setting up NTCR.
 
In Internet Service Manager (IIS1-3) or the Microsoft Management Console for IIS (IIS4 and up) select the directory you want to protect. Turn on Basic (Clear Text) and turn off Windows NT Challenge Response. It is okay to leave Allow Anonymous on.
 
When you select Basic (Clear Text) you will be warned that your Windows NT usernames and passwords will be transmitted without being encrypted. For your NT accounts this is a pretty serious issue. You should only consider this option in combination with SSL, which is slow and requires you to buy a certificate from Verisign or Thawte (among others).
 
Create an account for each user to whom you want to give access, remove the permissions for "IUSR_machinename" from the directory, and add permissions for the users you added. Alternatively you could set up a group, permit access to that group, and add permitted users to that group. Remember the user will need execute rights if the directory has any ASP, ISAPI extensions, counters etc.
 
IIS Basic Authentication is the way to go if:
You won't want to use IIS Basic Authentication. Third party Basic Authentication filters
There are many third party authentication filters on the commercial market. The Guru uses and recommends AuthentiX. AuthentiX is a fast, filter based third party tool for IIS authentication developed by Flicks Software.
 
These filters allow you to protect content directories and individual files by asking for usernames and passwords held separately from the Windows NT usernames and passwords, ensuring the the security of your NT accounts.
 
How to set up a third party Basic Authentication filter
It is recommended that you carefully read the documentation for your authentication software and install it accordingly. In general, this is an easy task.
 
A third party Basic Authentication filter is the way to go if: You won't want to use a third party Basic Authentication filter if: Write your own Basic Authentication filter
Writing your own Basic Authentication filter is an option if you have the skills, resources and time to do it.
 
How to write your own Basic Authentication filter
You will need to build a dll that conforms to the ISAPI filter specification and has the following entry points: The GetFilterVersion function is the first entry point called by the Internet Information Server. In this function you set the IIS notifications that you want to receive, and any other first time setup tasks.
 
The HttpFilterProc function is called in response to the notifications set in GetFilterVersion and is where the work of the filter is actually done.
 
There are several excellent references to help develop an ISAPI filter.
 
Writing your own Basic Authentication filter is the way to go if: You won't want to write your own Basic Authentication filter if: Cookie based authentication with ASP pages
You can use the cookie based session variables of Active Server Pages to capture a username and password from a form, validate the username and password, then set a session variable to indicate the user has correctly logged in.
 
How to use cookie based authentication with ASP pages Cookie based authentication with ASP pages is the way to go if: You won't want cookie based authentication with ASP pages if: Self-authenticating scripts.
Self-authenticating scripts usually provide a single URL entry point, with parameters indicating the current state of the session and the content requested. Self-authenticating scripts can be written as ASP, CGI, Win-CGI, or ISAPI dlls, and other variations.
 
Definitions How to use self-authenticating scripts
There are too many variations to show how to create a self authenticating script in this tutorial, however they all share a common means of authenticating.
 
When a request comes in and the content to be displayed is protected by a Basic Authentication username and password, the script sends a 401 Access Denied message, indicating the realm, and some html that is displayed to the user when the login attempt fails.
 
A regular http reply looks like this:
 
HTTP/1.0 200 OK
Server: Microsoft-IIS/3.0
Date: Wed, 11 Mar 1999 16:31:52 GMT
Content-Type: text/html
Last-Modified: Wed, 18 Feb 1998 22:45:46 GMT
Content-Length: 1234
 
Content: Interesting Stuff

 
A 401 Access denied reply looks like this
 
HTTP/1.0 401 Access Denied
Content-type: text/html
Server: Microsoft-IIS/3.0
Date: Wed, 11 Mar 1999 16:35:47 GMT
WWW-Authenticate: Basic realm="Message in Popup"
 
Content: You cannot get in!

 
Once the script sends a 401 Access Denied message, the browser will pop up a dialog indicating the realm, and invite the user to enter a username and password. The user will not see the access denied content unless the login fails. Some browser keep retrying the pop-up dialog until it succeeds or the user escapes out, others only pop-up three times.
 
If the user types in a username and password the browser will send them to the server as a part of the http request header that looks like the following:
 
Authorization: Basic cGvcmU6cGRcmU=
 
The string "cGvcmU6cGRcmU=" is Base64 encoded.
The script will ask the server for this header by requesting the server variable HTTP_AUTHORIZATION, and decode it. The resulting string will be in the format username:password and the script can match these against acceptable values in order to determine whether to transmit the content or issue another 401 Denied.
 
In IIS4, the HTTP_AUTHORIZATION value may not be returned correctly by IIS. In MMC, select the directory in which the ASP page calling this function resides. If Basic (Clear Text) is off, and NTCR is on, then HTTP_AUTHORIZATION will not return the correct value. This problem did not occur in IIS3. Microsoft bug Case Number is SR X980 2166010 644. Recommended workaround is to either: Make sure that Allow Anonymous is checked.
 
Self-authenticating scripts is the way to go if: You won't want self-authenticating scripts if Certificate based authentication. Client certificates are an advanced form of authentication, and at this time they are still very much in their infancy with respect to compatibility and ease of use.
 
How to use certificate based authentication
Since this technology is still maturing, be sure to have the latest version of IIS4 installed on your system. There are several good references to help understand and use Client Certificate technology. Some articles that are recommended include: Certificate based authentication is the way to go if:
You won't want certificate based authentication if: The Guru wishes to thank Kevin Flick (http://www.flicks.com) for providing this article.