首页 > 解决方案 > 在 User.Identity 中返回 NameIdentifier 的错误值

问题描述

我正在使用 Asp.net core 3.1 和 User.Identity 来获取 UserId ,最近我们的一些用户报告了一个问题,他们在登录时看到的是另一个用户帐户,而不是看到自己的帐户。这是我认为导致此问题的代码:

User.Identity.FindFirstValue(ClaimTypes.NameIdentifier);

我使用的扩展:

        public static string FindFirstValue(this IIdentity identity, string claimType)
        {
            var claimsIdentity = identity as ClaimsIdentity;
            return claimsIdentity?.FindFirstValue(claimType);
        }
        public static string FindFirstValue(this ClaimsIdentity identity, string claimType)
        {
            return identity?.FindFirst(claimType)?.Value;
        }

其他信息:

  1. 缓存未激活
  2. 用户报告此问题的比例不到 5%
  3. User.Identity.FindFirstValue(ClaimTypes.NameIdentifier)在 ViewComponent 中使用

标签: c#asp.net-coreasp.net-core-identity

解决方案


推荐阅读