简体   繁体   English

在 ASP.NET Identity 2.0 中获取当前用户 id

[英]Get current user id in ASP.NET Identity 2.0

I just switched over to using the new 2.0 version of the Identity Framework.我刚刚转而使用新的 2.0 版身份框架。 In 1.0 I could get a user object by using manager.FindByIdAsync(User.Identity.GetUserId()) .在 1.0 中,我可以通过使用manager.FindByIdAsync(User.Identity.GetUserId())获得用户 object 。 The GetUserId() method does not seem to exists in 2.0. GetUserId()方法似乎在 2.0 中不存在。

Now all I can figure out is to use manager.FindByEmailAsync(User.Identity.Name) which references the username field in the users table.现在我能弄清楚的是使用manager.FindByEmailAsync(User.Identity.Name)引用用户表中的用户名字段。 In my application this is set to the same as the email field.在我的应用程序中,它设置为与 email 字段相同。

I can see this causing issues down the road when someone needs to update their email. Is there a way to get the current logged in user object based off an unchanging value (such as the id field) in the Identity 2.0 Framework?当有人需要更新他们的 email 时,我可以看到这会导致问题。有没有办法根据 Identity 2.0 Framework 中不变的值(例如 id 字段)获取当前登录的用户 object?

GetUserId() is an extension method on IIdentity and it is in Microsoft.AspNet.Identity.IdentityExtensions . GetUserId()是一个扩展方法IIdentity ,它是在Microsoft.AspNet.Identity.IdentityExtensions Make sure you have added the namespace with using Microsoft.AspNet.Identity; 确保using Microsoft.AspNet.Identity;添加了命名空间using Microsoft.AspNet.Identity; .

In order to get CurrentUserId in Asp.net Identity 2.0, at first import Microsoft.AspNet.Identity : 为了在Asp.net Identity 2.0中获取CurrentUserId,首先导入Microsoft.AspNet.Identity

C#: C#:

using Microsoft.AspNet.Identity;

VB.NET: VB.NET:

Imports Microsoft.AspNet.Identity


And then call User.Identity.GetUserId() everywhere you want: 然后在任何地方调用User.Identity.GetUserId()

strCurrentUserId = User.Identity.GetUserId()

This method returns current user id as defined datatype for userid in database (the default is String ). 此方法返回当前用户标识,作为数据库中userid的定义数据类型(默认为String )。

Just in case you are like me and the Id Field of the User Entity is an Int or something else other than a string, 万一你和我一样,用户实体的Id字段是一个Int或除字符串以外的其他东西,

using Microsoft.AspNet.Identity;

int userId = User.Identity.GetUserId<int>();

will do the trick 会做的

I used Claims to get the userId, username and email of the logged in user.我使用 Claims 获取登录用户的 userId、用户名和 email。

            var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // will give the user's userId
        //userName = User.FindFirstValue(ClaimTypes.Name); // will give the user's userName
        //email = User.FindFirstValue(ClaimTypes.Email);

I had the same issue. 我遇到过同样的问题。 I am currently using Asp.net Core 2.2. 我目前正在使用Asp.net Core 2.2。 I solved this problem with the following piece of code. 我用下面的代码解决了这个问题。

using Microsoft.AspNetCore.Identity;
var user = await _userManager.FindByEmailAsync(User.Identity.Name);

I hope this will be useful to someone. 我希望这对某人有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM