Browse Source

Track and update user's last sign-in time

pull/24278/head
maliming 4 months ago
parent
commit
6b7bfb1ce6
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 12
      modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSignInManager.cs
  2. 10
      modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs
  3. 3
      modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/TokenController.Password.cs

12
modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSignInManager.cs

@ -1,4 +1,7 @@
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
@ -120,4 +123,11 @@ public class AbpSignInManager : SignInManager<IdentityUser>
{
return await base.SignInOrTwoFactorAsync(user, isPersistent, loginProvider, bypassTwoFactor);
}
public override async Task SignInWithClaimsAsync(IdentityUser user, AuthenticationProperties authenticationProperties, IEnumerable<Claim> additionalClaims)
{
user.SetLastSignInTime(DateTimeOffset.UtcNow);
await UserManager.UpdateAsync(user);
await base.SignInWithClaimsAsync(user, authenticationProperties, additionalClaims);
}
}

10
modules/identity/src/Volo.Abp.Identity.Domain/Volo/Abp/Identity/IdentityUser.cs

@ -127,6 +127,11 @@ public class IdentityUser : FullAuditedAggregateRoot<Guid>, IUser, IHasEntityVer
/// </summary>
public virtual DateTimeOffset? LastPasswordChangeTime { get; protected set; }
/// <summary>
/// Gets or sets the last sign-in time for the user.
/// </summary>
public virtual DateTimeOffset? LastSignInTime { get; protected set; }
//TODO: Can we make collections readonly collection, which will provide encapsulation. But... can work for all ORMs?
/// <summary>
@ -409,6 +414,11 @@ public class IdentityUser : FullAuditedAggregateRoot<Guid>, IUser, IHasEntityVer
LastPasswordChangeTime = lastPasswordChangeTime;
}
public virtual void SetLastSignInTime(DateTimeOffset? lastSignInTime)
{
LastSignInTime = lastSignInTime;
}
[CanBeNull]
public virtual IdentityUserPasskey FindPasskey(byte[] credentialId)
{

3
modules/openiddict/src/Volo.Abp.OpenIddict.AspNetCore/Volo/Abp/OpenIddict/Controllers/TokenController.Password.cs

@ -405,6 +405,9 @@ public partial class TokenController
}
);
user.SetLastSignInTime(DateTimeOffset.UtcNow);
await UserManager.UpdateAsync(user);
return SignIn(principal, OpenIddictServerAspNetCoreDefaults.AuthenticationScheme);
}

Loading…
Cancel
Save