[英]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.User
的User
属性。
该User
是一个从cookie自动创建的ClaimsPrincipal
对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.