加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何在Identity 3.0中获取当前的UserId? User.GetUs

发布时间:2020-12-16 03:43:14 所属栏目:asp.Net 来源:网络整理
导读:我需要获得提出请求的用户ID. 在以前的Identity版本中,我可以这样做: User.Identity.GetUserId(); 但它似乎不再可用了. 还有类似的东西: User.GetUserId(); 但它始终返回null,即使正确设置了User.Identity.IsAuthenticated并且正确设置了User.Identity.Nam
我需要获得提出请求的用户ID.
在以前的Identity版本中,我可以这样做:

User.Identity.GetUserId();

但它似乎不再可用了.

还有类似的东西:

User.GetUserId();

但它始终返回null,即使正确设置了User.Identity.IsAuthenticated并且正确设置了User.Identity.Name.

我应该用它做什么?

编辑:
我的身份验证逻辑基于[default Visual Studio 2015 template],到目前为止我的身份并没有太大变化,所以如果你想检查它是如何完成的,你可以在我粘贴的github链接上看到它.

解决方法

根据我们的讨论,您的用户身份似乎不包含User.GetUserId()使用的正确声明.

以下是您可以手动设置NameIdentifier声明的方法:

// ClaimsIdentity identity = new ClaimsIdentity...

identity.AddClaim(ClaimTypes.NameIdentifier,user.Id);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读