Anonymous Identification in ASP.NET Core MVC

2k Views Asked by At

In the previous ASP.NET MVC, you can turn on the anonymous identification easily by adding 1 line in your web.config:

<anonymousIdentification enabled="true" />

We can use the anonymous identification, i.e., Request.AnonymousID to identify unauthenticated users on your site. This is pretty useful for eCommerce experience when you need to save the items in the shopping cart against visitors.

More info in: http://www.toplinestrategies.com/blogs/net/anonymous-identification-mvc

The Problem:

Request.AnonymousID comes from System.Web, and it's gone with ASP.NET Core.

Questions:

  1. How can we enable anonymous identification in ASP.NET Core MVC?
  2. If 1 is not possible, how would you "identify" visitors on your site?

Note: I don't want to use Sessions to store objects.

1

There are 1 best solutions below

0
On

I coded a solution on my own. It is a middleware for ASP.NET Core that mimics the old behavior.

You can find the package on NuGet as AnonymousId (ReturnTrue.AspNetCore.Identity.Anonymous) and the source code on GitHub.

I'm new to the whole world of ASP.NET Core so please let me know of any bug, improvement, advice, correction...

The basic usage is:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    app.UseAnonymousId();
    ....
}

public class HomeController : Controller
{
  public ViewResult Index()
  {
      string anonymousId = Request.Headers["AnonymousId"];
      ....
  }
}