简体   繁体   English

在 asp.net 核心 3.1 中使用匿名用户 ID,例如来自 .Net 4.x 的 Request.AnonymousID?

[英]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?是否有内置的方法来处理 asp.net 核心中的会话之间的基于 cookie 的匿名用户 ID,或者我是否需要滚动我自己的/一些第三方的东西? Just like Request.AnonymousID did in 4.x days.就像Request.AnonymousID在 4.x 天所做的那样。

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...我已经做了一堆搜索“Asp.net 核心匿名用户 id”和相关的东西,这似乎是其中之一,我觉得必须有一个很好的解决方案 - 但我只是不搜索正确的关键字...

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.我确实在 github/nuget 中找到了一个名为AnonymousID的东西,但是它已经很老了,并且有很多奇怪的依赖项,这让我厌倦了安装它。

I ended up taking https://github.com/aleripe/AnonymousId/tree/master/AnonymousId and just adapting it to 3.1 - and it works perfectly.我最终采用了https://github.com/aleripe/AnonymousId/tree/master/AnonymousId并将其调整为 3.1 - 它运行良好。

You can also use the AllowAnonymous attribute to allow access by non-authenticated users to individual actions.您还可以使用 AllowAnonymous 属性来允许未经身份验证的用户访问单个操作。 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.这将只允许经过身份验证的用户访问 AccountController,除了登录操作,每个人都可以访问,无论他们的身份验证或未经身份验证/匿名状态如何。

Reference: Simple authorization in ASP.NET Core参考: ASP.NET内核中的简单授权

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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