domanda simile: Need access more user properties in User.Indentity risposto da professionisti Microsoft in worklog CodePlex, come di seguito
"È possibile ottenere l'oggetto utente e fare uso .Email o User.PhoneNumber poiché queste proprietà sono appese al modello User "
Possiamo ottenere l'oggetto utente corrente dell'applicazione nell'identità ASP.Net come sotto, da lì è possibile accedere a tutte le proprietà dell'oggetto utente, seguiamo la stessa cosa con le web-app mvc5 fin d'ora.
//In Account controller like this
var currentUser = UserManager.FindById(User.Identity.GetUserId());
In altri controller è necessario aggiungere il seguente al controllore:
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
// Get the current logged in User and look up the user in ASP.NET Identity
var currentUser = manager.FindById(User.Identity.GetUserId());
Ora possiamo accedere a tutti i puntelli utente (telefono # e lingua), come di seguito
var phoneNumber = currentUser.PhoneNumber;
var userLanguage = currentUser.Language;
MODIFICA: Se vuoi recuperare lo stesso in qualsiasi view.cshtml o _Layout.cshtml allora dovresti fare come sotto
@using Microsoft.AspNet.Identity
@using Microsoft.AspNet.Identity.EntityFramework
@using YourWebApplication.Models
@{
var phoneNumber= string.Empty;
var userLanguage = string.Empty;
if (User.Identity.IsAuthenticated) {
var userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());
var manager = new UserManager<ApplicationUser>(userStore);
var currentUser = manager.FindById(User.Identity.GetUserId());
phoneNumber = currentUser.PhoneNumber;
userLanguage = currentUser.Language;
}
}
fonte
2014-11-10 07:47:58
È possibile creare una classe personalizzata che eredita da IIdentity o IPrincipal per memorizzare le nuove proprietà. http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal – DSR