Login Audit API

The Software Outline

Following on from creating a console application to export a list of users and their last login date as a CSV, this project is to create an API layer to sit between the database and the console application with the following end points:

  • Get all
  • Get by name (Full or partial)
  • Get by date range

User story

As a Sitecore developer

I want to create an API layer to sit between the Sitecore database and the console application

So that I can return a list of all Sitecore users and their last login date

Acceptance criteria

AC1

GIVEN I want to find out the last login date for all Sitecore users

WHEN I call the Login audit API

THEN the following data is returned as a JSON

  • Full name
  • Last login date

AC2

GIVEN I’ve called the Login audit API

WHEN I view the data returned

THEN data is returned in descending date order

Manchester Web Crafters

My approach

Firstly I took a course on Pluralsight Building Your First API with ASP.NET Core 2 using Entity Framework Core from scratch.

This gave me the base knowledge I needed to start building the API. I scaffolded the database using EFCore to connect my application to the database. This created a file which I called GMCContext that acts as the gateway to the database:

I then created a data model so I can extract the data I need for the API from the database as shown below:

I created an interface for the GetLoginAuditReports( ) method called ILoginAuditReportService:

I set up the program.cs file using a template provided by the GMC so my API was in line with their testing requirements and their guidelines. The file includes middleware that creates logs and adds health checks along with other methods.

It also enables swagger which is a browser GUI for testing APIs in a development environment:

I then created 3 endpoints for my API.

  • Get by name
  • Get by date
  • Get all

Get all

Get all gets all users information that’s stored in the last login database.

Get by name

Get by name gets all users with the inputted name or a partial name match.

Get by date range

Get by date gets all users within an inputted date range.

Manchester Web Crafters

Result of the program

Get all

Get by name

Get by date

Challenges I overcame

While writing this program I have limited knowledge of C# and writing APIs with EFCore, so I spent lots of time working on a course and also going through the documentation to figure out certain aspects of the code and how it all links together.

The most difficult part was creating the data model and scaffolding the database using EFCore as I’m more familiar with the classic SQL connection method.

I have learned lots about the C# syntax and how to link databased and also about different API features in C#.

I needed help getting the data out of Sitecore as this is a specialist CMS of which I have no prior knowledge. Because of this to progress further with my development I will be taking a Sitecore 10 course on Pluralsight to learn about Docker and Sitecore 10 Helix.

I then created a presentation to demonstrate to my team what I had learned from the pluralsight courses.

Download Presentation

More articles

Comments section

Read more

Login Audit API

Read more

Login Audit App

Read more