如何在 ASP.NET Core 中获取当前登录的用户 ID?

编程入门 行业动态 更新时间:2024-10-26 07:23:45
本文介绍了如何在 ASP.NET Core 中获取当前登录的用户 ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我之前使用 User.Identity.GetUserId() 使用 MVC5 完成了此操作,但这在这里似乎不起作用.User.Identity 没有 GetUserId() 方法.

I've done this before with MVC5 using User.Identity.GetUserId() but that doesn't seem to work here. The User.Identity doesn't have the GetUserId() method.

我正在使用 Microsoft.AspNet.Identity.

推荐答案

我包括使用 System.Security.Claims 并且我可以访问 GetUserId() 扩展方法

I included using System.Security.Claims and I could access the GetUserId() extension method

注意:我已经使用了 Microsoft.AspNet.Identity 但无法获得扩展方法.所以我想这两者必须相互结合使用

NB: I had the using Microsoft.AspNet.Identity already but couldn't get the extension method. So I guess both of them have to be used in conjunction with one another

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

编辑:这个答案现在已经过时了.查看 Soren 或 Adrien 的答案,了解在 CORE 1.0 中实现这一目标的过时方法

EDIT: This answer is now outdated. Look at Soren's or Adrien's answer for a dated way of achieving this in CORE 1.0

更多推荐

如何在 ASP.NET Core 中获取当前登录的用户 ID?

本文发布于:2023-11-14 13:26:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587337.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:用户   如何在   ASP   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!