Build a ToDo list application using .Net 5.0 Web API and Microsoft SQL Server

Sakhile Msibi
5 min readSep 16, 2021

--

Introduction

This article shows how to build a ToDo list application using .Net 5 Web API, JWT authentication and AspNetCore Identity. Microsoft SQL Server is used to view the database and tables. This is part 1 of a 2-part tutorial. Part 2 will use Angular to create the frontend of the ToDo list application. This article starts by stating user stories showing how a user want to use the application.

The article will show all the necessary steps that are required to build a complete ToDo application backend. In the end the article demonstrate that only logged in users are able to access the ToDo list end points.

Tools

  • Visual Studio 2019
  • Microsoft SQL Server

User Stories

Create a new project using Visual Studio 2019 community edition.

Choose the ASP.NET Core Web API template. The next step would be an option to give a project name and choose a location for the project.

Choose .NET 5.0 as the target framework, nose for the authentication type field and unselect the configure for HTTPS. Then click create to create the project.

The project template will have this structure.

Install the latest version of the below packages using NuGet package manager. Th NuGet package manager can be found by right clicking the project, in this example, the ToDoAPI, and select manage NuGet packages.

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.AspNetCore.Identity.EntityFrameworkCore
  • Microsoft.AspNetCore.Identity
  • Microsoft.AspNetCore.Authentication.JwtBearer
  • Microsoft.VisualStudio.Web.CodeGeneration.Design

Create an Authentication folder that will contain a ApplicationUser.cs class that will inherit the IdentityUser class and Response.cs class that will return a message and a status code when a user register or login to the application. The IdentityUser class is a part of AspNetCore Identity.

ApplicationUser.cs

Response.cs

Create a models folder Models that will contain a RegisterModel.cs class for user registration, a LoginModel.cs class for user login, UserRoles.cs for user roles and ToDoItemModel.cs for to do items. It will also contain the ApplicationDbContext.cs file that maps the models to the tables that will be created through migration.

The RegisterModel.cs, LoginModel.cs and UserRoles.cs will be bounded to the identity tables. This means that only the fields that are described in the models will be required when a user register and login to the application. The roles will show the roles that a user can have, e.g., “admin”.

RegisterModel.cs

LoginModel.cs

UserRoles.cs

ToDoItemModel.cs

ApplicationDbContext.cs

The folder structure of the project now looks as shown below.

Modify the appsettings.json file to add the connection string and the jwt token secrete string, valid issuer, which is the port for the backend server and valid audience which is the port for the frontend server.

n the ConfigureServices method in the Startup.cs file add the DbContext and show that the application uses the SQL Server (it can also use MySQL, Postgres, etc.) and add the connection string that was described in the appsettings.json file, add JwtBearer authentication and add AspNetCore Identity as shown below. In the Configure method in the Startup.cs file add that the application uses authentication.

Inside the Controllers folder create a Web API controller Authentication. This is done by right-clicking on the Controllers folder, select add, choose controller from the drop-down and then select API Controller — Empty.

AuthenticationController.cs

Create a Web API controller ToDoItem. This is done by right-clicking on the Controllers folder, select add, choose “New Scaffolded Item” from the drop-down and then select API Controller with actions, using Entity Framework.

Choose the ToDoItemModel for the model class, ApplicationDbContext for the data context class and a controller name.

ToDoItemController.cs

Add [Authorize] to allow only user who have logged in and have a valid JWT token to access the ToDoItem APIs.

Create a migration script by using “add-migration” command in the package manger console.

A migrations folder is created in the project. The folder structure of the project now looks as shown below.

Create a database and tables by using “update-database” command in the package manger console. Use the Microsoft SQL Server object explorer to see the created database and tables.

Use postman to test the APIs. If you try to access the ToDoItem before registering to the application, you get a 401 unauthorized status code.

Once a user has registered the user can login and get a valid JWT token.

This can then be used to access the ToDoItem Web APIs.

Conclusion

In this article, I showed how to build a ToDo list application backend using .Net 5.0. I also showed how to use JWT token to authorize only logged in users to use the ToDo application. In the next article we going to implement the frontend using Angular. You can find the source code in my GitHub repository: https://github.com/Sakhile-Msibi/ToDoListApp.

--

--

Sakhile Msibi

Software Developer who is based in Gauteng, South Africa