如何在aspnetusers表中获取自定义字段?

Vas*_*kin 3 asp.net-mvc visual-studio asp.net-identity-2

使用CodeFirst迁移,我在VS 2013中的asp mvc模板中的标准数据库中的表aspnetusers中添加了自定义字段"Surname"(对不起"in").如果我登录,如何获得"姓氏"字段的价值?

Sam*_*ari 6

在任何操作方法中,您都可以通过调用User.Identity.GetUserId()扩展方法获取当前用户ID .你可以通过这样的方式获得额外的信息ApplicationUserManager:

public ActionResult MyAction()
{
    string surname = HttpContext.GetOwinContext()
        .GetUserManager<ApplicationUserManager>()
        .FindById(User.Identity.GetUserId()).Surname;
}
Run Code Online (Sandbox Code Playgroud)

确保添加了Microsoft.AspNet.Identity.Owin名称空间以使用扩展方法.