I have this this code
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password)
I want this code piece to run Synchronously, because my very next statement is depend upon this. Below call fails most of the time because user is null.
var roles = await userManager.GetRolesAsync(user.Id);
Any suggestion?
There is nothing wrong with the code.
await
means that execution continues only after the asynchronous method that follows finishes. This means that in this snippet:the call to
GetRolesAsync
will be executed only after the previous line completes.From the documentation of UserManager.FindAsync
Either the username or password are wrong. Just return a message to the end user asking them to retry. If you make this call using stored credentials, check them again.
In any case, you need to check for an authentication failure before trying to use the
user
value, eg: