In today’s blog, we are going to talk about a problem I stumbled across recently while working on one of my MVC 5 based web projects. How can we identify an anonymous user that interacts with our webpage over a period of time that exceeds the scope of a regular user session?
Our Example Scenario “I Remember You”
Let’s imagine a website that remembers the last time a user accessed it. As soon as users open the page, the website will tell them the exact date and time of their last visit within the last year. There is no sign-up option, so everything needs to work on an anonymous base.
If we open the page for the very first time, we should see something like:
On every other visit, the page should immediately recognize us and give us the timestamp of our last access:
Obviously that scenario has been solved many times in the past. Most people will immediately suggest to set a long-living cookie with a unique value on the user’s browser and use that cookie to associate its value with the user specific data (in our case a simple timestamp). And that is exactly what we are going to do. However, we are not going to reinvent the wheel and implement the cookie management logic ourselves. We, rather, leverage a functionality that is already built into the MVC framework.
The AnonymousID Property
If we explore the Request property that is provided by the standard MVC Controller class, we notice a property called AnonymousID.
That particular field does exactly what we need: It assigns and maintains a unique ID for each browser that accesses our web application. All the cookie management logic happens behind the scene, so we can use that functionality almost straight out of the box.
By default, anonymous identification is disabled in MVC. Consequently, the value of AnonymousID will never be populated and will always remain null. Fortunately, it is very easy to enable the anonymous identification functionality by performing a slight change in our web.config file:
<anonymousIdentification enabled="true" />
After that line is added to the web.config, the AnonymousId will be populated and will have the same value for each individual browser. The underlying cookie that is being generated by MVC has a default timespan of 100000 minutes (around 69 days). Fortunately MVC gives us a lot of ways to customize the way the anonymous identification module works under the hood. For our purposes let’s increase the cookie lifespan to 1 year (525600 minutes) to meet our initial “I Remember You” requirement.
<anonymousIdentification enabled="true" cookieTimeout="525600"/>
(For a complete documentation on the <anonymousIdentification> tag you can refer to the official MSDN page https://msdn.microsoft.com/en-us/library/91ka2e6a%28v=vs.85%29.aspx)
With the anonymous identification activated in our application’s web.xml, the controller for our “I Remember You” application turns out to be quite simple:
public class HomeController : Controller
public ActionResult Index()
DateTime? lastVisited = GetLastVisitForUser(Request.AnonymousID);
// Returns null if id wasn't found
public DateTime? GetLastVisitForUser(string id)
// ... Get from our chosen data source
public void SetLastVisitForUser(string id, DateTime dateTime)
//... Update or create entry in data source
For the sake of completeness here is the corresponding view markup:
@if (Model == null)
<b>This is your first visit. Welcome!</b>
<b>Your last visit was on @Model.Value.ToString("g"). Welcome back!</b>
About the Author:
TopLine Strategies delivers the complete integration and development of sales, marketing and customer service technologies that enable corporate clientele to improve revenue streams and strengthen customer interactions. Our project management and consulting is designed to achieve timely delivery, 100 percent user adoption of the technologies we implement and deliver measurable returns on investments for our clients.