【问题标题】:How to access cookie asp.net core如何访问 cookie asp.net core
【发布时间】:2017-03-07 04:34:30
【问题描述】:
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 值。

【问题讨论】:

    标签: cookies asp.net-core httpcontext


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 2017-08-12
      • 2016-07-10
      • 2020-11-20
      • 2017-07-22
      • 2021-07-05
      相关资源
      最近更新 更多