Reputation: 21506
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:
Note: I don't want to use Sessions to store objects.
Upvotes: 5
Views: 2224
Reputation: 3760
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"];
....
}
}
Upvotes: 6