ASP.NET Identity - Find user by email and password

Asked

Viewed 208 times

1

I’m using Asp.Net Identity for the first time, along with the Entity Framework, and to create a user, I do the following code:

var identityUser = new IdentityUser
{
   UserName = viewModel.Nome.Replace(" ", "."),
   Email = viewModel.Email
};

IdentityResult resultado = userManager.Create(identityUser, viewModel.Senha);

Therefore, the user is free to enter his name in the first field viewModel.Nome and your email in the second viewModel.Email. To return this user at login, I use the following code:

var usuario = userManager.Find(viewModel.Nome, viewModel.Senha);

My question is: Is it possible to search the user by E-mail, and not by Name? that is, at the time of the Find change the viewModel.Nome for viewModel.Email?

  • Responding in a simple way, yes. Or, if you like, the version synchronous.

  • @Isaac used an approach based on the link you passed, with some changes. Thank you.

1 answer

0


Problem solved. For this, I recovered the user by email, and soon after I logged in using his name, as follows:

var retorno = userManager.FindByEmail(viewModel.Login);
var usuario = userManager.Find(retorno.UserName, viewModel.Senha);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.