Skip to content

Commit d09ad4a

Browse files
author
Adit Sheth
committed
Added few missing code and changes.
1 parent f3116b0 commit d09ad4a

File tree

4 files changed

+41
-33
lines changed

4 files changed

+41
-33
lines changed

src/Identity/Extensions.Core/src/LockoutOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ public class LockoutOptions
3737
/// Specifies whether the lockout should be permanent.
3838
/// If true, the user will be locked out indefinitely.
3939
/// </summary>
40-
public bool PermanentLockout { get; set; } = false;
40+
public bool PermanentLockout { get; set; }
4141
}

src/Identity/Extensions.Core/src/PublicAPI.Shipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ Microsoft.AspNetCore.Identity.LockoutOptions.AllowedForNewUsers.get -> bool
184184
Microsoft.AspNetCore.Identity.LockoutOptions.AllowedForNewUsers.set -> void
185185
Microsoft.AspNetCore.Identity.LockoutOptions.DefaultLockoutTimeSpan.get -> System.TimeSpan
186186
Microsoft.AspNetCore.Identity.LockoutOptions.DefaultLockoutTimeSpan.set -> void
187+
Microsoft.AspNetCore.Identity.LockoutOptions.PermanentLockout.get -> bool
188+
Microsoft.AspNetCore.Identity.LockoutOptions.PermanentLockout.set -> void
187189
Microsoft.AspNetCore.Identity.LockoutOptions.LockoutOptions() -> void
188190
Microsoft.AspNetCore.Identity.LockoutOptions.MaxFailedAccessAttempts.get -> int
189191
Microsoft.AspNetCore.Identity.LockoutOptions.MaxFailedAccessAttempts.set -> void

src/Identity/Extensions.Core/src/UserManager.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,39 +1812,39 @@ public virtual async Task<IdentityResult> SetLockoutEndDateAsync(TUser user, Dat
18121812
/// </summary>
18131813
/// <param name="user">The user whose failed access count to increment.</param>
18141814
/// <returns>The <see cref="Task"/> that represents the asynchronous operation, containing the <see cref="IdentityResult"/> of the operation.</returns>
1815-
public virtual async Task<IdentityResult> AccessFailedAsync(TUser user)
1816-
{
1817-
ThrowIfDisposed();
1818-
var store = GetUserLockoutStore();
1819-
ArgumentNullThrowHelper.ThrowIfNull(user);
1820-
1821-
// If this puts the user over the threshold for lockout, lock them out and reset the access failed count
1822-
var count = await store.IncrementAccessFailedCountAsync(user, CancellationToken).ConfigureAwait(false);
1823-
if (count < Options.Lockout.MaxFailedAccessAttempts)
1815+
public virtual async Task<IdentityResult> AccessFailedAsync(TUser user)
18241816
{
1825-
return await UpdateUserAsync(user).ConfigureAwait(false);
1826-
}
1827-
Logger.LogDebug(LoggerEventIds.UserLockedOut, "User is locked out.");
1817+
ThrowIfDisposed();
1818+
var store = GetUserLockoutStore();
1819+
ArgumentNullThrowHelper.ThrowIfNull(user);
18281820

1829-
// Prevent overflow when setting lockout end date
1830-
var now = DateTimeOffset.UtcNow;
1831-
DateTimeOffset lockoutEnd;
1821+
// If PermanentLockout is enabled, lock the user indefinitely
1822+
if (Options.Lockout.PermanentLockout)
1823+
{
1824+
Logger.LogDebug(LoggerEventIds.UserLockedOut, "User is permanently locked out.");
1825+
await store.SetLockoutEndDateAsync(user, DateTimeOffset.MaxValue, CancellationToken).ConfigureAwait(false);
1826+
return await UpdateUserAsync(user).ConfigureAwait(false);
1827+
}
18321828

1833-
if (Options.Lockout.DefaultLockoutTimeSpan == TimeSpan.MaxValue)
1834-
{
1835-
lockoutEnd = DateTimeOffset.MaxValue;
1836-
}
1837-
else
1838-
{
1839-
lockoutEnd = now > (DateTimeOffset.MaxValue - Options.Lockout.DefaultLockoutTimeSpan)
1829+
// Increment access failed count
1830+
var count = await store.IncrementAccessFailedCountAsync(user, CancellationToken).ConfigureAwait(false);
1831+
if (count < Options.Lockout.MaxFailedAccessAttempts)
1832+
{
1833+
return await UpdateUserAsync(user).ConfigureAwait(false);
1834+
}
1835+
1836+
Logger.LogDebug(LoggerEventIds.UserLockedOut, "User is locked out.");
1837+
1838+
// Set the lockout time based on configuration
1839+
var now = DateTimeOffset.UtcNow;
1840+
DateTimeOffset lockoutEnd = Options.Lockout.DefaultLockoutTimeSpan == TimeSpan.MaxValue
18401841
? DateTimeOffset.MaxValue
18411842
: now.Add(Options.Lockout.DefaultLockoutTimeSpan);
1842-
}
18431843

1844-
await store.SetLockoutEndDateAsync(user, lockoutEnd, CancellationToken).ConfigureAwait(false);
1845-
await store.ResetAccessFailedCountAsync(user, CancellationToken).ConfigureAwait(false);
1846-
return await UpdateUserAsync(user).ConfigureAwait(false);
1847-
}
1844+
await store.SetLockoutEndDateAsync(user, lockoutEnd, CancellationToken).ConfigureAwait(false);
1845+
await store.ResetAccessFailedCountAsync(user, CancellationToken).ConfigureAwait(false);
1846+
return await UpdateUserAsync(user).ConfigureAwait(false);
1847+
}
18481848

18491849
/// <summary>
18501850
/// Resets the access failed count for the specified <paramref name="user"/>.

src/Identity/test/Identity.Test/UserManagerTest.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,13 +1013,18 @@ public async Task ResetTokenCallNoopForTokenValueZero()
10131013
}
10141014

10151015
[Fact]
1016-
public async Task AccessFailedAsync_IncrementsAccessFailedCount()
1016+
public async Task AccessFailedAsyncIncrementsAccessFailedCount()
10171017
{
10181018
// Arrange
10191019
var user = new PocoUser() { UserName = "testuser" };
10201020
var store = new Mock<IUserLockoutStore<PocoUser>>();
1021-
store.Setup(x => x.GetAccessFailedCountAsync(user, It.IsAny<CancellationToken>())).ReturnsAsync(1);
1022-
store.Setup(x => x.IncrementAccessFailedCountAsync(user, It.IsAny<CancellationToken>())).ReturnsAsync(2);
1021+
int failedCount = 1; // Simulated access failed count
1022+
1023+
store.Setup(x => x.GetAccessFailedCountAsync(user, It.IsAny<CancellationToken>()))
1024+
.ReturnsAsync(() => failedCount); // Return the updated value dynamically
1025+
1026+
store.Setup(x => x.IncrementAccessFailedCountAsync(user, It.IsAny<CancellationToken>()))
1027+
.ReturnsAsync(() => ++failedCount); // Increment and return the new count
10231028

10241029
var manager = MockHelpers.TestUserManager(store.Object);
10251030

@@ -1029,8 +1034,9 @@ public async Task AccessFailedAsync_IncrementsAccessFailedCount()
10291034
// Assert
10301035
IdentityResultAssert.IsSuccess(result);
10311036
store.Verify(x => x.IncrementAccessFailedCountAsync(user, It.IsAny<CancellationToken>()), Times.Once);
1032-
var failedCount = await manager.GetAccessFailedCountAsync(user);
1033-
Assert.Equal(2, failedCount);
1037+
1038+
var newFailedCount = await manager.GetAccessFailedCountAsync(user);
1039+
Assert.Equal(2, newFailedCount); // Ensure the count was actually updated
10341040
}
10351041

10361042
[Fact]

0 commit comments

Comments
 (0)