获取MVC Core中当前登录的用户ID [重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我试过谷歌搜索,但所有的结果似乎是旧版本。

我正在尝试获取登录用户的ID,然后将其与另一个表进行比较。在(sorta)明文我的意思是这样的:

 If (the current logged in user's id == Goals.UserId)
 {
     show all the logged in user's goal
 }
c# asp.net-core
1个回答
0
投票

如果你正在使用Asp.Net Core Identity,你可以使用他们的extension方法来简化对它的访问:

using Microsoft.AspNet.Identity;
[…]

if (User.Identity.GetUserId() == Goals.UserId)
{
    // show all the logged in user's goal
}
© www.soinside.com 2019 - 2024. All rights reserved.