If you need to get the user from within the controller, use the User property of Controller. If you need it from the view, I would populate what you specifically need in the ViewData , or you could just call User as I think it's a property of ViewPage .
HttpContext.User.Identity.Name is empty.
If you're coding in an ASP.NET MVC Controller, use
using Microsoft.AspNet.Identity;
...
User.Identity.GetUserId();
Worth mentioning that User.Identity.IsAuthenticated
and User.Identity.Name
will work without adding the above mentioned using
statement. But GetUserId()
won't be present without it.
If you're in a class other than a Controller, use
HttpContext.Current.User.Identity.GetUserId();
In the default template of MVC 5, user ID is a GUID stored as a string.
No best practice yet, but found some valuable info on extending the user profile:
Identity
: https://devblogs.microsoft.com/aspnet/introducing-asp-net-identity-a-membership-system-for-asp-net-applications/
Try something like:
var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
var userManager = new UserManager<ApplicationUser>(store);
ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;
Works with RTM.
If you want the ApplicationUser object in one line of code (if you have the latest ASP.NET Identity installed), try:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
You'll need the following using statements:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
Getting the Id is pretty straight forward and you've solved that.
Your second question though is a little more involved.
So, this is all prerelease stuff right now, but the common problem you're facing is where you're extending the user with new properties ( or an Items collection in you're question).
Out of the box you'll get a file called IdentityModel
under the Models folder (at the time of writing). In there you have a couple of classes; ApplicationUser
and ApplicationDbContext
. To add your collection of Items
you'll want to modify the ApplicationUser
class, just like you would if this were a normal class you were using with Entity Framework. In fact, if you take a quick look under the hood you'll find that all the identity related classes (User, Role etc...) are just POCOs now with the appropriate data annotations so they play nice with EF6.
Next, you'll need to make some changes to the AccountController
constructor so that it knows to use your DbContext.
public AccountController()
{
IdentityManager = new AuthenticationIdentityManager(
new IdentityStore(new ApplicationDbContext()));
}
Now getting the whole user object for your logged in user is a little esoteric to be honest.
var userWithItems = (ApplicationUser)await IdentityManager.Store.Users
.FindAsync(User.Identity.GetUserId(), CancellationToken.None);
That line will get the job done and you'll be able to access userWithItems.Items
like you want.
HTH
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With