Reputation: 4054
I have an Article entity in my project which has the ApplicationUser
property named Author
. How can I get the full object of currently logged ApplicationUser
? While creating a new article, I have to set the Author
property in Article
to the current ApplicationUser
.
In the old Membership mechanism it was simple, but in the new Identity approach I don't know how to do this.
I tried to do it this way:
using Microsoft.AspNet.Identity;
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == User.Identity.GetUserId());
But I get the following exception:
LINQ to Entities does not recognize the method 'System.String GetUserId(System.Security.Principal.IIdentity)' method, and this method cannot be translated into a store expression. Source=EntityFramework
Upvotes: 260
Views: 363362
Reputation: 477
There is a simple way :
User.Identity.Name
It provides the Id of the current user.
Upvotes: 1
Reputation: 4054
My mistake, I shouldn't have used a method inside a LINQ query.
Correct code:
using Microsoft.AspNet.Identity;
string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
Upvotes: 65
Reputation: 4587
In case someone is working with Identity
users in web forms
, I got it working by doing so:
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = manager.FindById(User.Identity.GetUserId());
Upvotes: 1
Reputation: 424
I was successfully available to get Application User By Following Piece of Code
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var user = manager.FindById(User.Identity.GetUserId());
ApplicationUser EmpUser = user;
Upvotes: -1
Reputation: 1537
ApplicationDbContext context = new ApplicationDbContext();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
ApplicationUser currentUser = UserManager.FindById(User.Identity.GetUserId());
string ID = currentUser.Id;
string Email = currentUser.Email;
string Username = currentUser.UserName;
Upvotes: 6
Reputation: 1082
Right now the asp.mvc project template creates an account controller that gets the usermanager this way:
HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>()
The following works for me:
ApplicationUser user = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(User.Identity.GetUserId());
Upvotes: 3
Reputation: 1501
As of ASP.NET Identity 3.0.0, This has been refactored into
//returns the userid claim value if present, otherwise returns null
User.GetUserId();
Upvotes: 6
Reputation: 93611
That introduces a new dependency of having an extra context for starters, but going forward the user database tables change (3 times in the past 2 years) but the API is consistent. For example the users
table is now called AspNetUsers
in Identity Framework, and the names of several primary key fields kept changing, so the code in several answers will no longer work as-is.
Another problem is that the underlying OWIN access to the database will use a separate context, so changes from separate SQL access can produce invalid results (e.g. not seeing changes made to the database). Again the solution is to work with the supplied API and not try to work-around it.
The correct way to access the current user object in ASP.Net identity (as at this date) is:
var user = UserManager.FindById(User.Identity.GetUserId());
or, if you have an async action, something like:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
FindById
requires you have the following using statement so that the non-async UserManager
methods are available (they are extension methods for UserManager, so if you do not include this you will only see FindByIdAsync
):
using Microsoft.AspNet.Identity;
If you are not in a controller at all (e.g. you are using IOC injection), then the user id is retrieved in full from:
System.Web.HttpContext.Current.User.Identity.GetUserId();
If you are not in the standard Account controller you will need to add the following (as an example) to your controller:
/// <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 update to Identity framework changes one of the underlying classes used for authentication. You can now access it from the Owin Context of the current HttpContent.
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
When using EF and Identity Framework with Azure, over a remote database connection (e.g. local host testing to Azure database), you can randomly hit the dreaded “error: 19 - Physical connection is not usable”. As the cause is buried away inside Identity Framework, where you cannot add retries (or what appears to be a missing .Include(x->someTable)
), you need to implement a custom SqlAzureExecutionStrategy
in your project.
Upvotes: 474
Reputation: 2165
For MVC 5 just look inside the EnableTwoFactorAuthentication method of ManageController in WebApplication template scaffold, its being done there:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> EnableTwoFactorAuthentication()
{
await UserManager.SetTwoFactorEnabledAsync(User.Identity.GetUserId(), true);
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
if (user != null)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
}
return RedirectToAction("Index", "Manage");
}
The answer is right there as suggested by Microsoft itself:
var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
It will have all the additional properties you defined in ApplicationUser class.
Upvotes: 3
Reputation: 342
The code of Ellbar works! You need only add using.
1 - using Microsoft.AspNet.Identity;
And... the code of Ellbar:
2 - string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
With this code (in currentUser
), you work the general data of the connected user, if you want extra data... see this link
Upvotes: 11
Reputation: 13125
Its in the comments of the answers but nobody has posted this as the actual solution.
You just need to add a using statement at the top:
using Microsoft.AspNet.Identity;
Upvotes: 33