Problem
In my project, I have an Article object with the ApplicationUser property named Author. What is the best way to access the complete object of the currently logged ApplicationUser? I have to set the Author property in Article to the current ApplicationUser while creating a new article.
It was simple with the old Membership system, but I’m not sure how to do it with the new Identity technique.
This is how I attempted it:
However, there is one exception:
Asked by Ellbar
Solution #1
For starters, this imposes a new dependence of having an extra context, however the user database tables will change in the future (3 times in the last 2 years), but the API will remain consistent. For example, in Identity Framework, the users table is now known as AspNetUsers, and the names of some primary key fields have changed, thus the code in previous responses will no longer function.
Another issue is that the underlying OWIN database access will utilise a different context, so modifications from independent SQL access may result in incorrect results (e.g. not seeing changes made to the database). Again, the solution is to use the provided API rather than attempting to work around it.
As of this date, the correct technique to retrieve the current user object in ASP.Net identity is:
var user = UserManager.FindById(User.Identity.GetUserId());
Likewise, if you’re using an async action, anything along the lines of:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
FindById requires the following using statement in order for the non-async UserManager methods to be available (they are UserManager extension methods, so if you don’t include this, you’ll only see FindByIdAsync):
using Microsoft.AspNet.Identity;
If you’re not in a controller at all (for example, if you’re using IOC injection), the user id is obtained in its entirety from:
System.Web.HttpContext.Current.User.Identity.GetUserId();
If you’re not using the standard Account controller, you’ll need to add the following to your controller (as an example):
/// <summary>
/// Application DB context
/// </summary>
protected ApplicationDbContext ApplicationDbContext { get; set; }
/// <summary>
/// User manager - attached to application DB context
/// </summary>
protected UserManager<ApplicationUser> UserManager { get; set; }
this.ApplicationDbContext = new ApplicationDbContext();
this.UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(this.ApplicationDbContext));
Update March 2015
Note: The most recent Identity Framework upgrade modifies one of the underlying authentication classes. You can now access it from the current HttpContent’s Owin Context.
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
You can get the dreaded “error: 19 – Physical connection is not useable” while utilizing EF and Identity Framework with Azure over a remote database connection (e.g. local host testing to Azure database). Because the root of the problem is buried deep within Identity Framework, where you can’t add retries (or what looks to be a missing. Include(x->someTable)), your project will require a specific SqlAzureExecutionStrategy.
Answered by Gone Coding
Solution #2
I made a mistake by using a method within a LINQ query.
Correct code:
using Microsoft.AspNet.Identity;
string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
Answered by Ellbar
Solution #3
It’s in the answers’ comments, but no one has actually put it as a solution.
Simply add a using statement to the head of the file:
using Microsoft.AspNet.Identity;
Answered by rtpHarry
Solution #4
Ellbar’s code is correct! All you have to do now is add using.
1 – using Microsoft.AspNet.Identity; 2 – using Microsoft.AspNet.Identity; 3 – using Microsoft.A
And here’s Ellbar’s code:
2 – ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId); string currentUserId = User.Identity.GetUserId(); ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
If you desire extra data, you can use this code (in currentUser) to deal with the connected user’s general info. look at this website
Answered by Diego Borges
Solution #5
This has been refactored into ASP.NET Identity 3.0.0.
//returns the userid claim value if present, otherwise returns null
User.GetUserId();
Answered by Seth IK
Post is based on https://stackoverflow.com/questions/20925822/asp-net-mvc-5-identity-how-to-get-current-applicationuser