I am building an ASP.NET MVC application and need to get and edit the current user logged in the application. I use the build in IdentityModel and coded a couple of get and edit methods on the AccountController in a similar way as presented here: How to get current user, and how to use User class in MVC5?
AccountController methods:
[NonAction]
public async Task<ApplicationUser> getCurrentUser(string userid)
{
var user = (ApplicationUser)await UserManager.FindByIdAsync(userid);
return user;
}
[NonAction]
public async Task<int> editCurrentUser(ApplicationUser user)
{
await UserManager.UpdateAsync(user);
return 1;
}
My problem is when I try to call them from another controller, in this case to update the balance
attribute from the payments controller:
var currentUser = await Controllers.AccountController.getCurrentUser(User.Identity.GetUserId());
because AccountController getCurrentUser and editCurrentUser are not static I cannot access them from the class. Quite obvious, but where does the MVC application create an instance of AccountController and how do I access it?
Alternatively, a better approach to retrieve and modify the current user?