Convert User to ApplicationUser in ASP.Net MVC

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:

image

You may come across requirement to work with ApplicationUser which is created as follows:

image

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.

One response to “Convert User to ApplicationUser in ASP.Net MVC”

  1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com