如何在ASP.NET Core中获取当前登录的用户ID [英] How to get the current logged in user Id in ASP.NET Core

查看:834
本文介绍了如何在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 doesnt 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆