简体   繁体   中英

Using an anonymous user id in asp.net core 3.1, like Request.AnonymousID from .Net 4.x?

Is there a built-in way to handle a cookie-based Anonymous UserId between sessions in asp.net core, or do I need to roll my own / some third part thing? Just like Request.AnonymousID did in 4.x days.

Background

I have done a bunch of searching for "Asp.net core anonymous user id" and related things, and this seems like one of those things, that I feel like there must be a great solution for - but I am just not searching for the right keywords...

I did find something in github/nuget called AnonymousID , but is is quite old and has a lot of strange dependencies, that makes me weary to install it.

I ended up taking https://github.com/aleripe/AnonymousId/tree/master/AnonymousId and just adapting it to 3.1 - and it works perfectly.

You can also use the AllowAnonymous attribute to allow access by non-authenticated users to individual actions. For example:

 [Authorize] public class AccountController: Controller { [AllowAnonymous] public ActionResult Login() { } public ActionResult Logout() { } }

This would allow only authenticated users to the AccountController, except for the Login action, which is accessible by everyone, regardless of their authenticated or unauthenticated / anonymous status.

Reference: Simple authorization in ASP.NET Core

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM