Asp.net 如何在Identity 3.0中获取当前用户ID?User.GetUserId返回null

Asp.net 如何在Identity 3.0中获取当前用户ID?User.GetUserId返回null,asp.net,asp.net-core,asp.net-identity-3,Asp.net,Asp.net Core,Asp.net Identity 3,我需要获取发出请求的用户的ID。 在以前的Identity版本中,我可以这样做: User.Identity.GetUserId(); 但它似乎不再可用了 还有类似的情况: User.GetUserId(); 但它始终返回null,即使User.Identity.IsAuthenticated为true并且User.Identity.Name设置正确 我应该用什么来做 编辑: 我的authenticaton逻辑是基于,到目前为止,我的身份没有太大变化,所以如果你想检查它是如何完成的,你可以在

我需要获取发出请求的用户的ID。 在以前的Identity版本中,我可以这样做:

User.Identity.GetUserId();
但它似乎不再可用了

还有类似的情况:

User.GetUserId();
但它始终返回null,即使User.Identity.IsAuthenticated为true并且User.Identity.Name设置正确

我应该用什么来做

编辑:
我的authenticaton逻辑是基于,到目前为止,我的身份没有太大变化,所以如果你想检查它是如何完成的,你可以在我粘贴的github链接上看到它。

我认为在以前的版本中有一个内置的扩展方法,但他们删除了它。您可以实现自己的功能来替换它:

using System;
using System.Security.Claims;
using Microsoft.AspNet.Identity;

namespace cloudscribe.Core.Identity
{
    public static class ClaimsPrincipalExtensions
    {
        public static string GetUserId(this ClaimsPrincipal principal)
        {
            if (principal == null)
            {
                throw new ArgumentNullException(nameof(principal));
            }
            var claim = principal.FindFirst(ClaimTypes.NameIdentifier);
            return claim != null ? claim.Value : null;
        }
    }
}

ClaimType.NameIdentifier应该映射到userid

User.Identity.GetUserId()在继承
页面的类中可用

您也可以使用

User.Identity.Name;
有关更多信息,请访问MSDN


在我们的讨论之后,您的用户标识似乎不包含user.GetUserId()使用的正确声明

以下是如何手动设置NameIdentifier声明:

// ClaimsIdentity identity = new ClaimsIdentity...

identity.AddClaim(ClaimTypes.NameIdentifier, user.Id);
@乔·奥德特

原来它已经被转移到其他地方了

User.GetUserId => UserManager.GetUserId(User)
User.GetUserName => UserManager.GetUserName(User)
User.IsSignedIn => SignInManager.IsSignedIn(User)

我的目标是在自定义中间件中检索用户ID这就是我使用的:

httpContext.User.Identity.IsAuthenticated ? 
    httpContext.User.Claims.Where(c => c.Type == ClaimTypes.NameIdentifier).First().Value 
    : Guid.Empty.ToString()


是的,但我需要获得ID;)不是。Identity 3.0主体不是Windows主体,我只是研究了一下,但由于问题标记为asp.net,我决定留下我的答案。虽然我没有注意到ID要求。那个继承“Controller”的类呢?你们在用什么?WebForms或MVC?GetUserId扩展确实存在于RC1中,但在中,它们似乎已将其删除。请查看我的答案。您可以发布您的身份验证逻辑吗?GetUserId从用户声明中检索Id。@Hazye您到底想要什么?我的项目是基于默认模板()的,到目前为止我没有做太多更改,您可以在这里查看一下。我已经使用您提供的模板测试了User.GetUserId(),效果很好。请确保当前用户的ClaimsIdentity包含nameidentifier声明,因为这是上面的方法能够检索用户id的方式。@Hazye嗯,它没有。我现在意识到,我正在编写的代码使用基于令牌的身份验证(针对API),它基于以下答案:。User.GetUserId在使用原始/内置身份验证的控制器中运行良好,但是当我使用我链接的身份验证时,没有nameidentifier和User.GetUserId。看起来我需要实现它。@Hezye您知道如何设置nameidentifier声明以使它最终工作吗?是的,看起来它工作了。最后谢谢但我仍然不明白在整个认证过程中到底发生了什么。它是如何工作的,以及关于当前活动用户的所有这些数据存储在哪里。你知道一些关于它的好消息来源吗?这不是问题的一部分,但似乎你知道一些关于这方面的事情。。。也许你可以给我推荐一些很棒的东西。关于用户的数据存储在授权服务器为每个用户生成的令牌(例如用户Id)中。然后,客户机/用户通过HTTP请求头(web api)或cookie(mvc)将该令牌传递给服务器。然后,服务器能够验证和解密令牌以获取用户信息。你可以在这里阅读关于基于索赔的身份:是的,这种变化发生在rc2中。在我看来,在ClaimsPrincipal上创建扩展方法似乎比在最新的项目中需要将UserManager和SignInManager注入到_LoginPartial.cshtml等视图中更好template@JoeAudette嗯,老实说,我很喜欢你的方式。这是更好的答案,在你不使用专有索赔协议的情况下也能起作用。这里有许多建议使用ClaimTypes.NameIdentifier常量,如果使用OpenID声明,则该常量无效。标识符声明在本例中是“sub”,而不是“sub”。