@@ -40,6 +40,7 @@ public class TwoFactorAuthenticationController : AccountBaseController
40
40
private readonly IDistributedCache _distributedCache ;
41
41
private readonly UrlEncoder _urlEncoder ;
42
42
private readonly ShellSettings _shellSettings ;
43
+ private readonly ITwoFactorAuthenticationHandlerCoordinator _twoFactorHandlerCoordinator ;
43
44
private readonly IHtmlLocalizer H ;
44
45
private readonly IStringLocalizer S ;
45
46
@@ -54,7 +55,8 @@ public TwoFactorAuthenticationController(
54
55
INotifier notifier ,
55
56
IDistributedCache distributedCache ,
56
57
UrlEncoder urlEncoder ,
57
- ShellSettings shellSettings )
58
+ ShellSettings shellSettings ,
59
+ ITwoFactorAuthenticationHandlerCoordinator twoFactorHandlerCoordinator )
58
60
: base ( userManager )
59
61
{
60
62
_signInManager = signInManager ;
@@ -65,6 +67,7 @@ public TwoFactorAuthenticationController(
65
67
_distributedCache = distributedCache ;
66
68
_urlEncoder = urlEncoder ;
67
69
_shellSettings = shellSettings ;
70
+ _twoFactorHandlerCoordinator = twoFactorHandlerCoordinator ;
68
71
H = htmlLocalizer ;
69
72
S = stringLocalizer ;
70
73
}
@@ -139,7 +142,7 @@ public async Task<IActionResult> LoginWithTwoFactorAuthentication(LoginWithTwoFa
139
142
if ( result . IsLockedOut )
140
143
{
141
144
_logger . LogWarning ( "User account locked out." ) ;
142
- ModelState . AddModelError ( String . Empty , S [ "The account is locked out" ] ) ;
145
+ ModelState . AddModelError ( String . Empty , S [ "The account is locked out. " ] ) ;
143
146
await _accountEvents . InvokeAsync ( ( e , user ) => e . IsLockedOutAsync ( user ) , user , _logger ) ;
144
147
145
148
return RedirectToAction ( nameof ( AccountController . Login ) , typeof ( AccountController ) . ControllerName ( ) ) ;
@@ -202,8 +205,6 @@ public async Task<IActionResult> LoginWithRecoveryCode(LoginWithRecoveryCodeView
202
205
203
206
var result = await _signInManager . TwoFactorRecoveryCodeSignInAsync ( recoveryCode ) ;
204
207
205
- var userId = await _userManager . GetUserIdAsync ( user ) ;
206
-
207
208
if ( result . Succeeded )
208
209
{
209
210
await _accountEvents . InvokeAsync ( ( e , user ) => e . LoggedInAsync ( user ) , user , _logger ) ;
@@ -221,7 +222,7 @@ public async Task<IActionResult> LoginWithRecoveryCode(LoginWithRecoveryCodeView
221
222
return RedirectToAction ( nameof ( AccountController . Login ) , typeof ( AccountController ) . ControllerName ( ) ) ;
222
223
}
223
224
224
- _logger . LogWarning ( "Invalid recovery code entered for user with ID '{UserId}' " , userId ) ;
225
+ _logger . LogWarning ( "Invalid recovery code entered for user." ) ;
225
226
ModelState . AddModelError ( String . Empty , S [ "Invalid recovery code entered." ] ) ;
226
227
}
227
228
@@ -241,7 +242,7 @@ public async Task<IActionResult> EnableAuthenticator(string returnUrl)
241
242
var user = await _userManager . GetUserAsync ( User ) ;
242
243
if ( user == null )
243
244
{
244
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
245
+ return NotFound ( "Unable to load user." ) ;
245
246
}
246
247
247
248
var model = await LoadSharedKeyAndQrCodeUriAsync ( user , loginSettings ) ;
@@ -266,7 +267,7 @@ public async Task<IActionResult> EnableAuthenticator(EnableAuthenticatorViewMode
266
267
var user = await _userManager . GetUserAsync ( User ) ;
267
268
if ( user == null )
268
269
{
269
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
270
+ return NotFound ( "Unable to load user." ) ;
270
271
}
271
272
272
273
if ( ! ModelState . IsValid )
@@ -322,7 +323,7 @@ public async Task<IActionResult> Index()
322
323
var user = await _userManager . GetUserAsync ( User ) ;
323
324
if ( user == null )
324
325
{
325
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
326
+ return NotFound ( "Unable to load user." ) ;
326
327
}
327
328
328
329
var model = new TwoFactorAuthenticationViewModel ( )
@@ -331,7 +332,7 @@ public async Task<IActionResult> Index()
331
332
IsTwoFaEnabled = await _userManager . GetTwoFactorEnabledAsync ( user ) ,
332
333
IsMachineRemembered = await _signInManager . IsTwoFactorClientRememberedAsync ( user ) ,
333
334
RecoveryCodesLeft = await _userManager . CountRecoveryCodesAsync ( user ) ,
334
- CanDisableTwoFa = ! loginSettings . RequireTwoFactorAuthentication
335
+ CanDisableTwoFa = ! await _twoFactorHandlerCoordinator . IsRequiredAsync ( )
335
336
|| ! await loginSettings . CanEnableTwoFactorAuthenticationAsync ( role => _userManager . IsInRoleAsync ( user , role ) ) ,
336
337
} ;
337
338
@@ -353,7 +354,7 @@ public async Task<IActionResult> ForgetTwoFactorClient()
353
354
var user = await _userManager . GetUserAsync ( User ) ;
354
355
if ( user == null )
355
356
{
356
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
357
+ return NotFound ( "Unable to load user." ) ;
357
358
}
358
359
359
360
await _signInManager . ForgetTwoFactorClientAsync ( ) ;
@@ -375,7 +376,7 @@ public async Task<IActionResult> GenerateRecoveryCodes()
375
376
var user = await _userManager . GetUserAsync ( User ) ;
376
377
if ( user == null )
377
378
{
378
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
379
+ return NotFound ( "Unable to load user." ) ;
379
380
}
380
381
381
382
var isTwoFactorEnabled = await _userManager . GetTwoFactorEnabledAsync ( user ) ;
@@ -405,11 +406,10 @@ public async Task<IActionResult> GenerateRecoveryCodesPost()
405
406
var user = await _userManager . GetUserAsync ( User ) ;
406
407
if ( user == null )
407
408
{
408
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
409
+ return NotFound ( "Unable to load user." ) ;
409
410
}
410
411
411
- var isTwoFactorEnabled = await _userManager . GetTwoFactorEnabledAsync ( user ) ;
412
- if ( ! isTwoFactorEnabled )
412
+ if ( ! await _userManager . GetTwoFactorEnabledAsync ( user ) )
413
413
{
414
414
await _notifier . ErrorAsync ( H [ "Cannot generate recovery codes for user because they do not have 2FA enabled." ] ) ;
415
415
@@ -437,7 +437,7 @@ public async Task<IActionResult> ShowRecoveryCodes()
437
437
var user = await _userManager . GetUserAsync ( User ) ;
438
438
if ( user == null )
439
439
{
440
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
440
+ return NotFound ( "Unable to load user." ) ;
441
441
}
442
442
443
443
var userId = await _userManager . GetUserIdAsync ( user ) ;
@@ -468,7 +468,7 @@ public async Task<IActionResult> ResetAuthenticator()
468
468
var user = await _userManager . GetUserAsync ( User ) ;
469
469
if ( user == null )
470
470
{
471
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
471
+ return NotFound ( "Unable to load user." ) ;
472
472
}
473
473
474
474
return View ( ) ;
@@ -490,7 +490,7 @@ public async Task<IActionResult> ResetAuthenticatorPost()
490
490
var user = await _userManager . GetUserAsync ( User ) ;
491
491
if ( user == null )
492
492
{
493
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
493
+ return NotFound ( "Unable to load user." ) ;
494
494
}
495
495
496
496
await _userManager . SetTwoFactorEnabledAsync ( user , false ) ;
@@ -514,10 +514,10 @@ public async Task<IActionResult> DisableTwoFactorAuthentication()
514
514
var user = await _userManager . GetUserAsync ( User ) ;
515
515
if ( user == null )
516
516
{
517
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
517
+ return NotFound ( "Unable to load user." ) ;
518
518
}
519
519
520
- if ( loginSettings . RequireTwoFactorAuthentication
520
+ if ( await _twoFactorHandlerCoordinator . IsRequiredAsync ( )
521
521
&& await loginSettings . CanEnableTwoFactorAuthenticationAsync ( role => _userManager . IsInRoleAsync ( user , role ) ) )
522
522
{
523
523
await _notifier . WarningAsync ( H [ "Two-factor authentication cannot be disabled for the current user." ] ) ;
@@ -544,10 +544,10 @@ public async Task<IActionResult> DisableTwoFactorAuthenticationPost()
544
544
var user = await _userManager . GetUserAsync ( User ) ;
545
545
if ( user == null )
546
546
{
547
- return NotFound ( $ "Unable to load user with ID ' { _userManager . GetUserId ( User ) } ' .") ;
547
+ return NotFound ( "Unable to load user." ) ;
548
548
}
549
549
550
- if ( loginSettings . RequireTwoFactorAuthentication
550
+ if ( await _twoFactorHandlerCoordinator . IsRequiredAsync ( )
551
551
&& await loginSettings . CanEnableTwoFactorAuthenticationAsync ( role => _userManager . IsInRoleAsync ( user , role ) ) )
552
552
{
553
553
await _notifier . WarningAsync ( H [ "Two-factor authentication cannot be disabled for the current user." ] ) ;
0 commit comments