ASP.NET Core 3.1 - Created And Last Login Date

Ken Haggerty
Created 02/20/2021 - Updated 02/23/2021 23:39

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

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

User Index
User Index Mobile
User Details Modal
User Details Modal Mobile
Update 02/23/2021

I updated the article links.

Comment Count = 0

Please log in to comment.

Login Register
Logged in users receive web notifications.
Web Notifications