Search code examples
asp.net-mvcvisual-studioasp.net-identity-2

How get custom field in aspnetusers table?


Using CodeFirst migration, i added custom field "Surname" in table aspnetusers in standart db in asp mvc template in VS 2013(sorry for "in"). How get value of "Surname" field, if i logged in?


Solution

  • In any action method you could get current user id by calling User.Identity.GetUserId() extension method. And you could get extra information by make use of ApplicationUserManager like this:

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

    Make sure you added Microsoft.AspNet.Identity.Owin namespace to could use the extension method.