Anonymous Identification in MVC

Printer-friendly version

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


Example controller

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);

            SetLastVisitForUser(Request.AnonymousID, DateTime.Now);

            return View(lastVisited);


        // 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:

@model DateTime?


    @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.

Comments (1)

Ryan replied on

Thank you for this!

Related Blogs

TheReact Native Open Source roadmap was announced in Q4 2018 after they decided to invest more in the React Native open source community.

October is not just about pumpkins, fall foliage, and cooler temps anymore. October 2018 also means the exciting introduction of Microsoft Dynamics 365 for Customer Engagement.

Back in 2016, Microsoft introduced its intentions to refresh its CRM and ERP strategy with Dynamics 365. At the heart of its services was the Common Data Model (CDM).