Calling the UserManager FindAsync Method

The new ASP.NET MVC 5 Project-template generates a prepared AccountController , which includes the basic implementation of UserManager and base stores ( IUserStore , IRoleStore , etc.).

This tutorial shows how to implement a custom UserStore to store user data in a user data source: http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider

However, I executed my own UserStore and found out the following:

During the login process, the UserManager.FindAsync(UserName, Password) method is called inside the AccountController :

 public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { **var user = await UserManager.FindAsync(model.UserName, model.Password);** //other code } return View(model); } 

The FindAsync method calls the basic UserStore methods in the following order: FindByNameAsync β†’ GetPasswordHashAsync β†’ FindByIdAsync .

My question is: why are both called FindByNameAsync and FindByIdAsync methods? This results in 2 database queries, which affects performance.

+6
source share
1 answer

When your UserStore retrieves a user using FindByNameAsync (), it simply finds and checks for the existence of the user.

When receiving user information, you will not return the password for the application, so the GetPasswordHashAsync () method calls the database again to get the hash code of your user.

0
source

All Articles