tracking login history of web matrix starter site template

Many newer sites are allowing users to see the last time they logged onto the system as an additional security feature.

I was not able to locate any functionality built into the web matrix 3.0 starter site template that would allow a user to view their last login history.

However with a few changes to the included database and the addition of some custom ‘helper’ code I was able to add this ability to a test site. The end result is a simple page as shown below.


enhancing web matrix 3 starter site template to show login activity

The first change to make is to the startersite database, create a LoginHistory table with UserId,LogonDate,Result fields (note: use int, datetime,nvarchar(20)) data types. Then what follows is some code to store login details.

How the code works is as follows in the accounts folder, in the page Login.cshtml, reference a web matrix helper function you can create similar to this. Around line 40 in the page, the email variable is assigned a value email = Request.Form["email"];.

We use this variable to pass a value to a ‘helper’ routine you can create some like this:

A helper would be created something like this.


The code could be placed in a file named LoginHistory.cshtml in the App_Code folder.

That code would be called from the login.chtml page for example:

How to display the logon activity? Create another helper routine kept in the same LoginHistory.cshtml file something like below:

The above series sample code is a starting point but needs more testing. Don’t use in production environments.