loading...
Cover image for Unit testing ASP.NET Core Identity

Unit testing ASP.NET Core Identity

samueleresca profile image Samuele Resca ・3 min read

Originally posted on : https://samueleresca.net

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.

Setup Project

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: https://gist.github.com/samueleresca/f4897bf1598ea116df04e1cb94ee05d5 In order to use ASP.NET Core Identity and OpenIddict add the following packages to your project:

https://gist.github.com/samueleresca/db20d8530e0a6785d0ea6c9c60671460

Setup Authentication

Create new Startup.Auth.cs file which will contain the setup of authentication: https://gist.github.com/samueleresca/622b50631610c9b9aa1f5eedcf9b613e The Startup.Auth.cs contains the partials Startup class and initialises identity environment:

  • adds the IdentityDbContext to the application services;
  • maps AppUser model 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: ASP.NET Core Identity In order to retrieve user data from data source, the application will use 4 key components:

  • AppUser defines the user data source model;
  • UserRepository connects services class to the data source. It uses DbContext in order to retrieve information from database;
  • UserService aggregates different providers: UserValidator, PasswordValidator, SignInManager; It is used by the UserController to obtain informations form database;
  • UsersController handles http requests form the client and retrieve information about users;

The following code shows the implementation of the UserController. The UserService and UserRepository are available on Github.

https://gist.github.com/samueleresca/143919b169408773885c176d657ecc7b Â

Unit test User APIs

Obviously, we need to cover UsersController using unit tests. The following project will use xUnit and Moq as mocking framework. Firstly, the UsersControllerTests defines two fake classes: FakeUserManager and FakeSignInManager, which will be used by the mocking framework:

https://gist.github.com/samueleresca/4780b1f9026675764c850d076ff750fa https://gist.github.com/samueleresca/c3cd952b3b0b5aff15ee11c63446f93f

In order to mock ASP.NET Core Identity, create a new Test server which will solves the application dependencies:

https://gist.github.com/samueleresca/b4e9a886e5f27917792f315e940bc464

Finally, we need to mock our FakeUserManager and FakeSignInManager classes by using Moq. The mocking will be implemented by the constructor (setup) of the UserControllerTest class:

https://gist.github.com/samueleresca/593d1b602dc1e951b7f64c2f433590a7 Â

Conclusion

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. Cheers :)

Posted on by:

samueleresca profile

Samuele Resca

@samueleresca

Samuele Resca is an Microsoft MVP Visual Studio and Development Technologies, Software Engineer, specializing mainly on ASP.NET MVC and in general about everything that revolves around the web. Samuele was born in 1994, and works as a software developer @ YOOX NET-A-PORTER Group He loves the MVC frameworks, ASP.NET MVC, Javascript, Node.js and Typescript.

Discussion

markdown guide