簡體   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