In the last post I talked about How to get current user in ASP.Net MVC 5 . Type of the current user is IPrincipal which is shown as follows:
You may come across requirement to work with ApplicationUser which is created as follows:
You can covert returned User to ApplicationUser as follows:
ApplicationDbContext db = new ApplicationDbContext(); ApplicationUser appUser = db.Users.FirstOrDefault(x => x.Id == userId); return appUser ;
To convert create instance of the ApplicationDBContext and then fetch user using LINQ to Entity. Returned user from context is of type ApplicationUser.
Hope it helps. Happy coding.
Your solution is Not working… here is my working
ApplicationUser appUser = db.Users.FirstOrDefault(x => x.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase));
Error:
Cannot implicitly covert from IdentityUser to ApplicationUser.