.net core ClaimTypes.NameIdentifier返回电子邮件地址而不是Id

.net core ClaimTypes.NameIdentifier返回电子邮件地址而不是Id,.net-core,httpcontext,.net Core,Httpcontext,我正在web API项目中使用HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value来获取请求用户,用于身份验证。NameIdentifier返回当前用户的电子邮件,而Google搜索建议它应该返回用户的Id(在本例中,如果这很重要,则返回Guid)。 为什么会发生这种情况?我如何在不进行数据库查询的情况下返回当前用户的id?自从生成这样的jwt令牌以来,我在使用ASP.net core 2.0时遇到了同样的问题 public Cl

我正在web API项目中使用
HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value
来获取请求用户,用于身份验证。NameIdentifier返回当前用户的电子邮件,而Google搜索建议它应该返回用户的Id(在本例中,如果这很重要,则返回Guid)。

为什么会发生这种情况?我如何在不进行数据库查询的情况下返回当前用户的id?

自从生成这样的jwt令牌以来,我在使用ASP.net core 2.0时遇到了同样的问题

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)
    });
}
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";
});
我试图通过
UserManager
获取
CurrentUser
,但它总是返回
null
。我想在启动文件中将
UserIdClaimType
字符串更改为“id”,如下所示

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)
    });
}
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";
});
var builder=services.AddIdentity(o=>
{
//配置标识选项
o、 Password.RequireDigit=false;
o、 Password.RequireLowercase=false;
o、 Password.RequireUppercase=false;
o、 Password.RequireNonAlphanumeric=false;
o、 Password.RequiredLength=6;
//这条线
o、 ClaimsIdentity.UserIdClaimType=“id”;
});

因此,当我遇到同样的问题时,我搜索了一下,根据Auth0.com论坛上的消息,出于某种原因,.NET中的JWT身份验证处理程序会将
声明映射到
名称标识符
声明类型


因此,解决方案不是设置
sub
声明类型,也不是将其设置为id。

应用程序使用什么身份验证机制?是什么造成了这个索赔?是.NETCore2.0吗?是的,是.NETCore2.0。我正在使用JwtBearer进行身份验证。我不熟悉ClaimsPrinciple,所以我假设JwtBearer正在处理这个问题?对不起,我指的是身份验证(使用JWTbeer作为jwt令牌),您找到这个问题的答案了吗?编辑:几秒钟后发现,ima对此添加了一个答案。我个人无法确认这是否有效,但我会将其标记为已接受的答案,因为这是一个如此古老的非活动问题;)哈哈,谢谢!:)