Creating a ASP.NET Core 2.1 project with identity configured you might miss controllers and many views that all have been available with ASP.NET Core 2.0 projects. All this functionality has been put into a Razor Class Library – a new feature with ASP.NET Core 2.1. You don’t have to deal with this code in your application. Not having the code you might ask how to customize the views. There’s an easy way to do this as shown here.
Identity with ASP.NET Core 2.0
Creating a new ASP.NET Core 2.0 project, using the project template with Visual Studio 2017, you can change the authentication to store accounts locally. With this option, many classes and views are created:
- Controller –
- Data –
- Extensions – extension methods for emails and SMS
- Models – several view-model types in
- Services –
- Views – used by the
This functionality allows registration of users, login with a password and 2FA (2 Factor Authentication), if users forgot the password login via recovery code, logout, changing the password…
ConfigureServices method of the
Startup class, the database context for storing all user information is specified with the
AddDbContext method specifying the
ApplicationDbContext class. Invoking the
AddIdentity extension method configures the
IdentityRole for user and role information, and maps it to the EC Core context. The method
AddDefaultTokenProviders configures the token providers for data protector, phone number, email, and authenticator. The registration of the service class
EmailSender for the
IEmailSender contract just contains an empty implementation – you need to create an implementation for the
SendMailAsync method to have functionality to send emails. The
Configure method registers middleware. With the method
AuthenticationMiddleware is used with every request.
Using Identity with ASP.NET Core 2.1
Creating the same with ASP.NET Core 2.1, the result is very different. The two controllers are missing, there are no views and no view-models. Just the EF Core context
ApplicationDbContext to map user and roles to the database are still here.
You can find the new folder Areas/Identity/Pages with the file _ViewStart.cshtml. This is a configuration for the identity pages coming from the library: the layout file part of the application is used by all the Razor pages in the Identity area. This is a simple way to customize the look of the views, it will use the same layout as used by the application.
The startup code has been simplified – and extended. For being GDPR conform, ASP.NET Core 2.1 adds support for cookie consent.
Where is the code? Customizing the UI
Where is the code for the controllers and the views? The implementation has been changed from the MVC architecture to Razor Pages. The Razor Pages are implemented in a library. The source code is available in the GitHub identity repository. The new NuGet package referenced is Microsoft.AspnetCore.Identity.UI.
Razor Pages allow code for views to be either in the file for the view itself, or a code-behind page.
In case you need more customization of the pages, you can access the source code at GitHub to create your own pages, or see what CSS classes are used for easy customization. There’s also an easy way from Visual Studio to customize the pages. All you need to do is open the context menu while selecting the application in the Solution Explorer, and selecting the menu Add | New Scaffold Item…. From there, select Identity to override the Identity pages.
The new dialog opened allows to override all the pages, or select the pages that you need to change.
With the sample code, Login and Register has been selected. You can find the generated pages within the Identity area. You can now completely customize the user interfaces along with the code-behind for the implementation.
Using ASP.NET Core 2.1, your code has been simplified by removing all the views and the controllers for authentication. Default functionality is coming from a library – a library making use of Razor Pages. In case you need customization other than having different styles for the HTML controls, you can easily create the pages with code-behind functionality by using Visual Studio 2017 and Scaffolding.
Read more about Razor Pages in the book Professional C# 7 and .NET Core 2.0.
Get the complete sample with ASP.NET Core 2.0 and 2.1 authentication from More Samples!
Enjoy programming and learning,
If you found this information valuable and want to return me a favor, then buy me a coffee.