Gathering a Web Site Users Information in ASP.NET

Gathering information about a Web site user is very common task for a web administrator. There are different ways to gather this information such as using log files, IIS logs etc. ASP.NET also provides classes that can be used to gather web site stats. In this article, I will show you how you can collect information about your site visitors using ASP.NET classes provided by the .NET Framework library.

In this my focus will be on one class - HttpRequest. The HttpRequest class provides properties that can be used to read the HTTP values sent by a client browser when a request is made. The HttpRequest class properties are briefly described in Table 1. As you can see from this table, you can get information about browser, application, contents, cookies, filters, headers, referrals, host and so on.

Table 1. HttpRequest class properties

AcceptTypes Returns a string array of client-supported MIME accept types
ApplicationPath Returns the ASP.NET application's virtual application root path on the server
Browser Returns information about the requesting client's browser capabilities
ClientCertificate Returns the current request's client security certificate
ContentEncoding Returns the character set of the entity-body
ContentLength Specifies the length, in bytes, of content sent by the client
ContentType Returns the MIME content type of the incoming request
Cookies Returns a collection of cookies sent by the client
CurrentExecutionFilePath Returns the virtual path of the current request
FilePath Returns the virtual path of the current request
Files Returns the collection of client-uploaded files (Multipart MIME format)
Filter Returns or sets the filter to use when reading the current input stream
Form Returns a collection of form variables
Headers Returns a collection of HTTP headers
HttpMethod Returns the HTTP data transfer method (such as GET, POST, or HEAD) used by the client
InputStream Returns the contents of the incoming HTTP entity body
IsAuthenticated Returns a value indicating whether the user has been authenticated
IsSecureConnection Returns a value indicting whether the HTTP connection uses secure sockets (that is, HTTPS)
Params Returns a combined collection of QueryString, Form, ServerVariables, and Cookies items
Path Returns the virtual path of the current request
PathInfo Returns additional path information for a resource with a URL extension
PhysicalApplicationPath Returns the physical file system path of the currently executing server application's root directory
PhysicalPath Returns the physical file system path corresponding to the requested URL
QueryString Returns the collection of HTTP query string variables
RawUrl Returns the raw URL of the current request
RequestType Returns or sets the HTTP data transfer method (GET or POST) used by the client
ServerVariables Returns a collection of Web server variables
TotalBytes Returns the number of bytes in the current input stream
Url Returns Information about the URL of the current request
UrlReferrer Returns information about the URL of the client's previous request that linked to the current URL
UserAgent Returns the raw user agent string of the client browser
UserHostAddress Returns the IP host address of the remote client
UserHostName Returns the DNS name of the remote client
UserLanguages Returns a sorted string array of client language preferences

The Request member of an ASP.NET web application represents the HttpRequest object. For example, if you want to know the application path, you can use the following code:
str = Request.ApplicationPath.ToString());

In the similar fashion, you can use other properties of HttpRequest to get the information related to a request. 
Gathering Clients Information
For test this, I create a Web application and add a button and Label control to the Web page. On the button click event handler, I write the code listed in Listing 1. As you can see from this code, I create a StringBuilder object and add strings to it by using the Request object properties. I add application path, host address, referral URL, authenticated, secured connection and browser related information to StringBuilder and then display the contents of it in the Label control using the Text property.
StringBuilder builder =
new StringBuilder();
builder.Append("<font face=\"verdana\" size=\"2\" >");
builder.Append("<br><b>Application Path</b>: " + Request.ApplicationPath.ToString());
builder.Append("<br><b>User Host</b>: " + Request.UserHostAddress.ToString()); builder.Append "<br><b>Referrer</b>: " + Request.UrlReferrer.ToString());
builder.Append("<br><b>Is Authenticated</b>: " +Request.IsAuthenticated.ToString()); 
builder.Append("<br><b>Is Secure</b>: " + Request.IsSecureConnection.ToString());
builder.Append("<br><b>Operating System:</b>: " + Request.Browser.Platform);
builder.Append( "<br><b>Browser</b>: " + Request.Browser.Browser);
builder.Append( "<br><b>Browser Version:</b>: " + Request.Browser.Version);
Lable1.Text = builder.ToString();

Looping through Server Variables
The ServerVariables property returns a NameValueCollection object. You can read all server variables by looping through this collection as you can see from the following code:
NameValueCollection coll =
StringBuilder builder =
new StringBuilder();
// Load ServerVariable collection into NameValueCollection object.
coll = Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (int i = 0; i < arr1.Length; i++)
builder.Append("Key: " + arr1[i] + "<br>");
String[] arr2 = coll.GetValues(arr1[i]);
for (int j = 0; j < arr2.Length; j++)
builder.Append("Value " + j + ": " + arr2[j] + "<br>");
Label1.Text = builder.ToString();

In the above code, I loop through all server variables provided by HttpRequest.Variables member and read them in a StringBuilder object. After that I simply display the contents of StringBuilder in a Label control using Label1.Text = builder.ToString();
Even though, HttpRequest class does not provide every little thing you need to know about your web site visitors but I am sure it provide enough functionality to find out who is visiting your web site, where are they coming from, and what operating system and browser they are using. In this article, we saw some of this functionality.
If you are interested in gathering more information such as downloads, pages, and files, you may want to look at the log files. You need to contact your system administrator to find out the correct path of the logs files. Once you know the correct path, you can open these log files (these files are text files) and parse them based on the information you are looking for.