如何访问cookieasp.net核心

How to access cookie asp.net core

var claims = new[]
{
  new Claim("UserID", user.ID.ToString()),
  new Claim(ClaimTypes.Role, "pioneer")
};

var principal = new ClaimsPrincipal(
                  new ClaimsIdentity(
                    claims, CookieAuthenticationDefaults.AuthenticationScheme));

await HttpContext.Authentication.SignInAsync("Cookies", principal);

这是我根据本教程中的说明创建并用于登录的 cookie:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie

正在创建 cookie,但我不知道如何访问 cookie 或其中的数据来获取 user.ID 值。

通过 HttpContext.Authentication.SignInAsync 签名后,您应该能够访问控制器中的 User 属性(它是 ControllerBase 的一部分),或者 HttpContext.User.

User 是一个 ClaimsPrincipal 从 cookie 自动创建的对象。