ASP.NET MVC 5 – Identity. How to get current ApplicationUser

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:

  • Add using statement for Identity extensions: using Microsoft.AspNet.Identity;
  • Then I try to get the current user: 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

10 Answers
10

Leave a Comment