HOWTO: Getting User Name and Password in ISAPI or CGI App

ID: Q140964

The information in this article applies to:

SUMMARY

This article explains how to retrieve values for the user name and password in an ISAPI or CGI application.

MORE INFORMATION

The user name and password values can be retrieved in the ISAPI/CGI, according to the following rules.

The user name can only be retrieved when either Basic authentication scheme or Microsoft Windows NT Challenge/Response schemes are used. You can get the name by using the server variable, REMOTE_USER.

In ISAPI you can use GetServerVariable() API, and in CGI you can use getenv() C run-time function.

The following sample code can be used to get the server variable:

....

// This will fail with ERROR_INSUFFICIENT_BUFFER,
// since we supplied NULL buffer. As a result, dwLength will
// indicate the size of the buffer to allocate
if (!pECB -> GetServerVariable (pECB -> ConnID,
                           "REMOTE_USER",
                           NULL,
                           &dwLength) )
{
    // Handle error other then ERROR_INSUFFICIENT_BUFFER here.
} lpszVar= (CHAR *) LocalAlloc (LPTR, dwLen); if ( !pECB -> GetServerVariable (pECB -> ConnID,
                                 "REMOTE_USER",
                                 lpszVar,
                                 &dwLength))
{
    // Handle error here
} ....

The user password can only be retrieved when Basic authentication scheme is used. The password is not available with the Windows NT Challenge/Response scheme, because the password never gets transmitted on the network.

To retrieve the password with the Basic authentication scheme, you need to parse it out from the HTTP_AUTHORIZATION server variable, which sets from HTTP Authorization header. HTTP_AUTHORIZATION has the following value:

   Basic xxxxxxxxxxxxxxx

where Basic is an authentication scheme used, then it is followed by a space, and "xxxxxxxxxxxxxxx" is UUENCODED string for "User-Name:User- Password" pair separated by the semicolon.

Notes:

For additional information, please see the HTTP protocol spec, available on http://www.w3.org.
Keywords          : kbnetwork iisapi 
Version           : 1.0
Platform          : NT WINDOWS
Issue type        : kbhowto

Last Reviewed: May 21, 1997