我怎样才能获得的当前登录的用户MVC5的具体领域? [英] How can I get the specific fields of the currently logged-in user in MVC5?

查看:163
本文介绍了我怎样才能获得的当前登录的用户MVC5的具体领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用个人身份鉴定,当然ASP.NET身份的MVC5应用程序。问题的关键是,我已经延长我有一个从ApplicationUser继承的典范,它只是像这样定义的:

I have an MVC5 app that uses Individual Authentication, and of course ASP.NET Identity. The point is that I had extended I have a model that inherits from ApplicationUser, it is simply defined like this:

public class NormalUser : ApplicationUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

那么,一点是,首先我要检查是否有登录的用户,如果有,我希望得到他/她的名字,姓氏和电子邮件领域。我怎样才能实现呢?

So, the point is that, first of all I want to check whether there is a logged-in user, and if there is, I want to get his/her FirstName, LastName and Email fields. How can I achieve it?

我想我需要使用这样的事情,检查是否有登录的用户:

I think I need to use something like this to check whether there is a logged-in user:

if (Request.IsAuthenticated)
{
    ...
}

可是,我怎样才能得到这些特定字段的值当前用户?

But, how can I get those specific fields' values for the current user?

推荐答案

是的,在身份,如果需要其他用户信息,只是拉从数据库中的用户,因为这是所有存储实际的用户对象上了。

Yes, in Identity, if you need additional user information, you just pull the user from the database, since this is all stored on the actual user object now.

if (Request.IsAuthenticated)
{
    var user = UserManager.FindById(User.Identity.GetUserId());
}

如果 GetUserId 不在 User.Identity ,以下内容添加到您的usings:

If GetUserId isn't on User.Identity, add the following to your usings:

using Microsoft.AspNet.Identity;

这篇关于我怎样才能获得的当前登录的用户MVC5的具体领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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