C
C#4mo ago
hutoanhill

✅ .NET Identity can't require 2FA

on registration i set up 2fa and require it to log in:
await userManager.SetTwoFactorEnabledAsync(user, true);

// get the newly defined key
string? key = await userManager.GetAuthenticatorKeyAsync(user);

// make sure we get the key.
if (string.IsNullOrEmpty(key)) {
await userManager.ResetAuthenticatorKeyAsync(user);
key = await userManager.GetAuthenticatorKeyAsync(user);

if (string.IsNullOrEmpty(key)) {
throw new NotSupportedException("The user manager must produce an authenticator key.");
}
}

// Return the shared key and indicate successful registration
return TypedResults.Ok(new RegistrationResponse(key));
await userManager.SetTwoFactorEnabledAsync(user, true);

// get the newly defined key
string? key = await userManager.GetAuthenticatorKeyAsync(user);

// make sure we get the key.
if (string.IsNullOrEmpty(key)) {
await userManager.ResetAuthenticatorKeyAsync(user);
key = await userManager.GetAuthenticatorKeyAsync(user);

if (string.IsNullOrEmpty(key)) {
throw new NotSupportedException("The user manager must produce an authenticator key.");
}
}

// Return the shared key and indicate successful registration
return TypedResults.Ok(new RegistrationResponse(key));
however when i go to login:
var result = await signInManager.PasswordSignInAsync(login.Email, login.Password, isPersistent, lockoutOnFailure: true);

if (result.RequiresTwoFactor) {
if (!string.IsNullOrEmpty(login.TwoFactorCode))
{
...

var result = await signInManager.PasswordSignInAsync(login.Email, login.Password, isPersistent, lockoutOnFailure: true);

if (result.RequiresTwoFactor) {
if (!string.IsNullOrEmpty(login.TwoFactorCode))
{
...

the initial sign in fails (as expected) and result.RequiresTwoFactor is false. what am i doing wrong?
3 Replies
hutoanhill
hutoanhillOP4mo ago
ok, this gets even stranger. i checked if 2FA was enabled rigth before sign in and it is enabled. do i jus tnot understand how signInManager.PasswordSignInAsync works?
hutoanhill
hutoanhillOP4mo ago
seems to imply that it works lik i think it does

RequiresTwoFactor

Returns a flag indication whether the user attempting to sign-in requires two factor authentication.

RequiresTwoFactor

Returns a flag indication whether the user attempting to sign-in requires two factor authentication.
i wish PasswordSignInAsync mentioned wether this flag means that the sign in was sucessful. ok. tested replaceing the value with true looks like theres a problem with my ability to sign in period.
Want results from more Discord servers?
Add your server