sipertypes.nameidentifier返回电子邮件地址而不是ID



我正在我的Web API项目中使用HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value来获取请求用户,以供身份验证。名称识别仪返回当前用户的电子邮件,而Google搜索表明它应该返回用户的ID(在这种情况下,如果有的话,GUID)。
为什么会发生这种情况,如何在没有数据库查询的情况下返回当前用户的ID?

,所以我在遇到同样的问题时搜索了一点,并且根据Auth0.com论坛上的答复,JWT Authentication Handler .NET中的JWT Authentication Handler,出于某种原因将映射sub索赔NameIdentifier索赔类型。

因此,解决方案没有设置sub声明类型,也不会设置为ID。

我在使用ASP.NET Core 2.0时遇到了相同的问题,因为我像这样生成JWT令牌

public ClaimsIdentity GenerateClaimsIdentity(string email, string id)
{
    return new ClaimsIdentity(new GenericIdentity(email, "Token"), new[]
    {
        new Claim(Helpers.Constants.Strings.JwtClaimIdentifiers.Id, id),
        new Claim(Helpers.Constants.Strings.JwtClaimIdentifiers.Rol, Helpers.Constants.Strings.JwtClaims.ApiAccess)
    });
}

我尝试通过UserManager获得CurrentUser,但它总是返回null。我已经想到将UserIdClaimType字符串更改为启动文件中的" ID"

var builder = services.AddIdentity<User,IdentityRole>(o =>
{
    // configure identity options
    o.Password.RequireDigit = false;
    o.Password.RequireLowercase = false;
    o.Password.RequireUppercase = false;
    o.Password.RequireNonAlphanumeric = false;
    o.Password.RequiredLength = 6;
    //this line
    o.ClaimsIdentity.UserIdClaimType = "id";
});

最新更新