ASP.NET Core 3.1 - Created And Last Login Date
This article will describe the implementation of created and last login dates for users. I will assume you have downloaded the ASP.NET Core 3.1 - Users Without Identity Project or created a new ASP.NET Core 3.1 Razor Pages project. See Tutorial: Get started with Razor Pages in ASP.NET Core. You should review the earlier articles of the Users Without Identity Project series.
Users Without Identity Project and Article Series
Creation Series
- ASP.NET Core 3.1 - Users Without Identity
- ASP.NET Core 3.1 - User Entity
- ASP.NET Core 3.1 - Password Hasher
- ASP.NET Core 3.1 - User Management
- ASP.NET Core 3.1 - Admin Role
- ASP.NET Core 3.1 - Cookie Validator
- ASP.NET Core 3.1 - Concurrency Conflicts
- ASP.NET Core 3.1 - Must Change Password
- ASP.NET Core 3.1 - User Database Service
- ASP.NET Core 3.1 - Rename Related Entities
2FA Series
- ASP.NET Core 3.1 - 2FA Without Identity
- ASP.NET Core 3.1 - 2FA User Tokens
- ASP.NET Core 3.1 - 2FA Cookie Schemes
- ASP.NET Core 3.1 - 2FA Authenticating
- ASP.NET Core 3.1 - 2FA Sign In Service
- ASP.NET Core 3.1 - 2FA QR Code Generator
- ASP.NET Core 3.1 - Admin 2FA Requirement
- ASP.NET Core 3.1 - 2FA Admin Override
Enhanced User Series
- ASP.NET Core 3.1 - Enhanced User Without Identity
- ASP.NET Core 3.1 - 2FA Recovery Codes
- ASP.NET Core 3.1 - Login Lockout
- ASP.NET Core 3.1 - Created And Last Login Date
- ASP.NET Core 3.1 - Security Stamp
- ASP.NET Core 3.1 - Token Service
- ASP.NET Core 3.1 - Confirmed Email Address
- ASP.NET Core 3.1 - Password Recovery
UWIP v2 implements CreatedDate and LastLoginDate properties for the AppUser.
Entities > AppUser:
[Display(Name = "Last Login")] public DateTimeOffset? LastLoginDate { get; set; } [Display(Name = "Created")] public DateTimeOffset CreatedDate { get; set; }
When a new AppUser is created, the CreatedDate is set to DateTimeOffset. UtcNow and the LastLoginDate defaults to null. The UserService implements an UpdateAppUserLastLoginDateAsync function which is called when the user successfully logs in. This function not only updates the LastLoginDate, but it also resets the AccessFailedCount to 0.
Services > UserService:
public async Task<bool> UpdateAppUserLastLoginDateAsync(int appUserId) { var appUser = await TrackAppUserByIdAsync(appUserId).ConfigureAwait(false); if (appUser == null) return false; appUser.LastLoginDate = DateTimeOffset.UtcNow; appUser.AccessFailedCount = 0; return await UpdateAppUserAsync(appUser, appUser.RowVersion).ConfigureAwait(false); }
The CreatedDate and the LastLoginDate are displayed on the user listing page and details modal
Update 02/23/2021
I updated the article links.
Comments(0)