0
var completedTask = await Task.WhenAny(_userManager.FindByNameAsync(userForLoginDto.UserName), _userManager.FindByIdAsync(userForLoginDto.Id)); 
var user = await completedTask; 
var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDto.Password, false);

I get an error

System.InvalidOperationException: A second operation started on this context before a previous operation completed.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • You cannot execute multiple commands simultaneous on/in the same db context. So `WhenAny` is a problem here. Also why not only use the id call. It is probably the fastest of the two anyway. – Silvermind Jan 18 '21 at 08:42
  • thanks you can take a look for this https://stackoverflow.com/questions/65665224/how-to-check-user-with-findbynameasync-and-findbyidasync-both-at-the-same-time/65665875?noredirect=1#comment116161189_65665875 – mohamedhemeda Jan 18 '21 at 08:46
  • I see. Well... You cannot do this on a db context. The question of _why_ remains. Why do you want to do this? – Silvermind Jan 18 '21 at 08:48
  • to check both of id and username with password to check the three fields to successfully login – mohamedhemeda Jan 18 '21 at 08:50
  • But you are not checking both, you are checking which one is the fastest. If you really need to check both you had to use `Task.WhenAll`, but you cannot, because it gives you the same problem. In this case you would have to do two separate calls. – Silvermind Jan 18 '21 at 10:08
  • thanks here the problem CheckPasswordSignInAsync must have only one variable as the first paramter – mohamedhemeda Jan 18 '21 at 11:00
  • Please explain why you need to check id and username and password. Normally you retrieve the user by username, if it is not null, you know the username exists. Then you check if the password is valid for that user. If that is valid, you know username and password are valid. You do not need the id. – Silvermind Jan 19 '21 at 08:10

0 Answers0