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.
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:
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
IdentityDbContextto the application services;
AppUsermodel class as identity class;
- configures the use of OpenIddict;
AppUserdefines 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:
SignInManager; It is used by the
UserControllerto obtain informations form database;
UsersControllerÂ handles http requests form the client and retrieve information about users;
The following code shows the implementation of the
UserRepository are available on Github.
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:
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. Cheers :)
There are a lot of people who love both JS and UX/CSS. If we stop labeling people just as “JS developers” or “UX developers”, we can achieve a ceasefire in the current “JS vs. CSS” war and achieve a mutually benefiting peace.