@@ -431,15 +431,15 @@ Replace the `Login` component. The following version of the `Login` component:
431431 </div>
432432 <div style="display:@(requiresTwoFactor ? "block" : "none")">
433433 <div class="form-floating mb-3">
434- <InputText @bind-Value="Input.TwoFactorCode "
435- id="Input.TwoFactorCode "
434+ <InputText @bind-Value="Input.TwoFactorCodeOrRecoveryCode "
435+ id="Input.TwoFactorCodeOrRecoveryCode "
436436 class="form-control"
437437 autocomplete="off"
438438 placeholder="###### or #####-#####" />
439- <label for="Input.TwoFactorCode " class="form-label">
439+ <label for="Input.TwoFactorCodeOrRecoveryCode " class="form-label">
440440 Two-factor Code or Recovery Code
441441 </label>
442- <ValidationMessage For="() => Input.TwoFactorCode "
442+ <ValidationMessage For="() => Input.TwoFactorCodeOrRecoveryCode "
443443 class="text-danger" />
444444 </div>
445445 </div>
@@ -478,19 +478,22 @@ Replace the `Login` component. The following version of the `Login` component:
478478 {
479479 if (requiresTwoFactor)
480480 {
481- if (!string.IsNullOrEmpty(Input.TwoFactorCode ))
481+ if (!string.IsNullOrEmpty(Input.TwoFactorCodeOrRecoveryCode ))
482482 {
483- // The [RegularExpression] data annotation ensures that the input will be either a six-digit
484- // authenticator code (######) or eleven-character alphanumeric recovery code (#####-#####)
485- if (Input.TwoFactorCode.Length == 6)
483+ // The [RegularExpression] data annotation ensures that the input
484+ // is either a six-digit authenticator code (######) or an
485+ // eleven-character alphanumeric recovery code (#####-#####)
486+ if (Input.TwoFactorCodeOrRecoveryCode.Length == 6)
486487 {
487488 formResult = await Acct.LoginTwoFactorCodeAsync(
488- Input.Email, Input.Password, Input.TwoFactorCode);
489+ Input.Email, Input.Password,
490+ Input.TwoFactorCodeOrRecoveryCode);
489491 }
490492 else
491493 {
492494 formResult = await Acct.LoginTwoFactorRecoveryCodeAsync(
493- Input.Email, Input.Password, Input.TwoFactorCode);
495+ Input.Email, Input.Password,
496+ Input.TwoFactorCodeOrRecoveryCode);
494497
495498 if (formResult.Succeeded)
496499 {
@@ -501,12 +504,22 @@ Replace the `Login` component. The following version of the `Login` component:
501504 }
502505 }
503506 }
507+ else
508+ {
509+ formResult =
510+ new FormResult
511+ {
512+ Succeeded = false,
513+ ErrorList = [ "Invalid two-factor code." ]
514+ };
515+ }
504516 }
505517 else
506518 {
507519 formResult = await Acct.LoginAsync(Input.Email, Input.Password);
508520 requiresTwoFactor = formResult.ErrorList.Contains("RequiresTwoFactor");
509- Input.TwoFactorCode = string.Empty;
521+ Input.TwoFactorCodeOrRecoveryCode = string.Empty;
522+
510523 if (requiresTwoFactor)
511524 {
512525 formResult.ErrorList = [];
@@ -536,21 +549,23 @@ Replace the `Login` component. The following version of the `Login` component:
536549 "eleven-character alphanumeric recovery code (#####-#####, dash " +
537550 "required)")]
538551 [Display(Name = "Two-factor Code or Recovery Code")]
539- public string TwoFactorOrRecoveryCode { get; set; } = string.Empty;
552+ public string TwoFactorCodeOrRecoveryCode { get; set; } = string.Empty;
540553 }
541554}
542555```
543556
544557Using the preceding component, the user is remembered after a successful login with a valid TOTP code from an authenticator app. If you want to always require a TOTP code for login and not remember the machine, call the ` TwoFactorRequestAsync ` method with ` TwoFactorRequest.ForgetMachine ` set to ` true ` immediately after a successful two-factor login:
545558
546559``` diff
547- if (Input.TwoFactorCode .Length == 6)
560+ if (Input.TwoFactorCodeOrRecoveryCode .Length == 6)
548561{
549- formResult = await Acct.LoginTwoFactorCodeAsync(Input.Email, Input.Password, Input.TwoFactorCode);
562+ formResult = await Acct.LoginTwoFactorCodeAsync(Input.Email, Input.Password,
563+ Input.TwoFactorCodeOrRecoveryCode);
550564
551565+ if (formResult.Succeeded)
552566+ {
553- + var forgetMachine = await Acct.TwoFactorRequestAsync(new() { ForgetMachine = true });
567+ + var forgetMachine =
568+ + await Acct.TwoFactorRequestAsync(new() { ForgetMachine = true });
554569+ }
555570}
556571```
0 commit comments