ASP.NET: Tracking/Identification of anonymous users

Some times there is a need to track the returning visitors even if they are anonymous. ASP.NET has a feature for this, called AnonymousID. We can use it in various situation like preventing shopping cart data, as a partial solution for preventing voting in polls more than once or as an id to use with ASP.NET Membership Profiles.

Configuration

By default, this feature is disabled and is not explicitly configured neither in web or machine.config. To enable it, add following in web.config:


For more information refer to anonymousIdentification Element (ASP.NET Settings Schema) entry in MSDN library.

One little thing not mentioned in docs, quite obvious (…after a few hours of scratching my head) is that in order this to work you need to be sure that authentication is set to Forms:

Usage

In my example I will assume that I would like to have a not very bullet proof method to prevent users from submitting the same form more than once. For the simplicity of this example I will use simple XML file and Linq to XML to store information about user rating of the sample picture.

First, lets create a new XML document with following contents:



I have created a simple form with 2 Labels, RequiredFieldValidator and RadioButtonList and a Button:

image.png

With everything in place its time to write the Button Click event, currents user AnonymousID is available as property of Request object (Request.AnonymousID):

protected void Button1_Click(object sender, EventArgs e)

{

XDocument x = XDocument.Load(Server.MapPath("~/results.xml"));

//checking if user has previously submitted his rating

if (AlreadyVoted(Server.MapPath("~/results.xml"), Request.AnonymousID))

{
Label2.Visible = true;

Label2.Text = "You have already voted.";

}

else

{

//adding the submited results

var result = new XElement("result");

result.Add(new XElement("userid", Request.AnonymousID));

result.Add(new XElement("datetime", DateTime.Now.ToShortDateString() + “ “ + DateTime.Now.ToShortTimeString()));

result.Add(new XElement("value", RadioButtonList1.SelectedValue));

x.Element("results").Add(result);

x.Save(Server.MapPath("~/results.xml"));

//calculating the average rating

string avg = (from q in x.Elements("results").Elements("result")

select (double)q.Element("value")).Average().ToString();

Label2.Visible = true;

Label2.Text = "Thank you for your vote. Current avarage:" + avg ;

}

}

Above code is quite obvious, if AlreadyVoted method return true, it means that user AnonymousID is in our records:

public bool AlreadyVoted(string path, string userid)

{

if (File.Exists(path))

{

XDocument xml = XDocument.Load(path);

bool q = (from uid in xml.Elements("results").Elements("result").Elements("userid")

where uid.Value == userid

select uid.Value

).Any();

return q;

}

return false;

}

After few submits the contents of the XML file are:



  
    5af9d0a8-0c7d-43f2-beb0-caff2783add8
    2009-05-03 01:29
    3
  
  
    77501434-9cbd-45f9-8c42-625dbfc2b471
    2009-05-03 01:31
    3
  
  
    16f17754-2998-4c03-a468-df6bce29e49d
    2009-05-03 01:48
    5
  

This is the simplest example of using the AnonymousID you could imagine. Full
source code: [download id="1"].
For more examples and details refer to HttpRequest.AnonymousID Property on MSDN library.

0 Responses to “ASP.NET: Tracking/Identification of anonymous users”


  • No Comments

Leave a Reply