In this multi part blog post we will be creating a .NET Core based Web API with authentication and a number of other features.

To start we will create a new project in Visual Studio by selecting the ASP.NET Core Web Application project type

then we select the API option on the next dialog

In this example we want to first implement authentication with a standard user and role table in our own database.

That is not an option in the template so we simply select ‘No Authentication’ and add it later on.

Click the OK button and wait for Visual Studio to create our base project. This will create a simple base web API with a sample values controller.

First thing we need to do is to configure the database connection. Add your connection string to the appsettings.json file

We also need to create a database context and register the context with the application

then in Startup.cs add the following to add the ApplicationDbContext

To create the base ASP.Net authentication tables in our database execute the following line in the console

this will create the migration steps for the initial creation of the authentication tables

now execute the following to run the migration

this will create all the tables in your database needed for authentication

In order to be able to extend the user table with additional fields we will create a model for our ApplicationUser

And of course we have to change all the references we have to IdenityUser to refer to the ApplicationUser instead.

Now we need to configure the project for identity and authentication

add the following code to the ConfigureServices method in StartUp to add the IdentoryService

then also in ConfigureServices we add the following to configure the IdentityOptions

and one more to configure the ApplicationCookie options

Next step is to make sure we have some default roles and a default user in our database. To do this we can add code to seed the database with our defaults

Then we add the following line to the Configure method

Lets create a class called SeedDataManager as follows

We can call the SeedData method from the configure method in Startup

We use Dependency Injection to injext the UserManager as well as the RoleManager, then call our SeedData method.

Now lets take a look at seeding the role table. For our example lets create a role called ‘User’ and another role called ‘Administrator’

and then we also create an admin user

next time you run the project the system will create the two roles as well as our default admin user

to test the authentication we can add another endpoint to the values controller as follows

calling the following endpoint

should return the following data

calling the new end point decorated with Authorize will return an HTTP status code of 401 because we are not authenticated

In Part 2 we will add an endpoint to authenticate a user with our web api

Michael Salzlechner is the CEO of StarZen Technologies, Inc.

He was part of the Windows Team at Data Access Worldwide that created the DataFlex for Windows Product before joining StarZen Technologies. StarZen Technologies provides consulting services as well as custom Application development and third party products