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
This question is related to
c#
asp.net-mvc-5
identity
asp.net-identity
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
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());
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.
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());
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();
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);
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;
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;
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;
Source: Stackoverflow.com