繁体   English   中英

如何访问cookie asp.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/zh-cn/aspnet/core/security/authentication/cookie

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

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

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

暂无
暂无

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

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