5

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.

David Liang
  • 20,385
  • 6
  • 44
  • 70
  • I don't have any .NET Core project that needs this needs yet. Have you tried to create a cookie with GUID value each time when the application starts up, and delete/refresh that cookie after the user logs out? `Request.AnonymousID` from `System.Web` is just a GUID. This is what I can think of for now (why am I answering my own question :)) – David Liang Jun 23 '17 at 18:24

1 Answers1

6

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"];
      ....
  }
}
Alessandro
  • 3,666
  • 2
  • 28
  • 41