ASP.NET Core Identity is a membership system which allows you to add login functionality to your application. Users can create an account and login with a user name and password or they can use an external login providers such as Facebook, Google, Microsoft Account, Twitter and more.
In the following article, you will learn how to implement and unit test ASP.NET Core Identity.
You can configure ASP.NET Core Identity to use a SQL Server database to store user names, passwords, and profile data. Alternatively, you can use your own persistent store to store data in another persistent storage, The following article will use SQL Server as data source engine.
The project described in the article will also use OpenIddict to implement token authentication: OpenIddict aims at providing a simple and easy-to-use solution to implement an OpenID Connect server in any ASP.NET Core application.
The following article will add ASP.NET Core Identity to the sample project used by: Implementing SOLID REST API using ASP.NET Core.
In order to use OpenIddict, add the appropriate MyGet repositories to your NuGet sources. This can be done by adding a new
NuGet.Config file at the root of your solution:
In order to use ASP.NET Core Identity and OpenIddict add the following packages to your project:
Startup.Auth.cs file which will contain the setup of authentication:
Startup.Auth.cs contains the partials Startup class and initialises identity environment:
- adds the
IdentityDbContextto the application services;
AppUsermodel class as identity class;
- configures the use of OpenIddict;
Retrieve data from data source
The following schema shows the API implementation, from Data access layer to the API layer:
In order to retrieve user data from data source, the application will use 4 key components:
AppUserdefines the user data source model;
UserRepositoryconnects services class to the data source. It uses DbContext in order to retrieve information from database;
UserServiceaggregates different providers:
SignInManager; It is used by the
UserControllerto obtain informations form database;
UsersControllerhandles http requests form the client and retrieve information about users;
The following code shows the implementation of the
UserRepository are available on Github.
Unit test User APIs
Obviously, we need to cover
UsersController using unit tests. The following project will use xUnit and Moq as mocking framework.
UsersControllerTests defines two fake classes:
FakeSignInManager, which will be used by the mocking framework:
In order to mock ASP.NET Core Identity, create a new Test server which will solves the application dependencies:
Finally, we need to mock our
FakeSignInManager classes by using Moq. The mocking will be implemented by the constructor (setup) of the
In conclusion, ASP.NET Core Identity is the out of box membership framework provided by ASP.NET Core. This article shows how to test the behaviour of the user authentication, you can find the complete project on GitHub.
Cover picture by Corrado Zeni.