0

I am writing a C# MVC5 internet application and am having some trouble getting the ApplicationUser object in a controller that I have created.

Here is my code:

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
ApplicationUser user = userManager.FindByNameAsync(User.Identity.GetUserId()).Result;

When I try to run this code, when a user is logged in, I am getting a null object for the ApplicationUser user.

Can I please have some help with this?

Thanks in advance

1 Answer 1

3

FindByNameAsync() expects a username but you're passing the user id. Either pass the username...

ApplicationUser user = userManager.FindByNameAsync(User.Identity.GetUserName()).Result;

Or use the FindByIdAsync() method...

ApplicationUser user = userManager.FindByIdAsync(User.Identity.GetUserId()).Result;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.