I’m working on a new .Net Core 5 web app with user authentication and Identity Server 4 where I need to customize some of the Identity account pages (Login, Register, Forgot Password, etc). Out of the box these pages are built into .Net Core and Identity Server 4 so there’s nothing you need to do to use them. However, if you want to customize any of the account pages you’ll need to scaffold the source of those pages into your project.
In the latest version of Visual Studio 2019 – (Version 16.8.4 as of today) you are able to do this by right clicking the project and selected “Add –> New Scaffolded Item” for all .Net Core projects below version 5 without any issues. But if you are trying to scaffold Identity in .Net Core 5 from Visual Studio then you might run into this error:
“There was an error running the selected code generator: ‘Package restored failed. Rolling back package changes for ‘Your App’.”
A way around this is error I found was to use the dotnet CLI outside of Visual Studio. Follow these steps to scaffold the full Identity pages into your .Net Core 5 app.
- Close Visual Studio
- Open a command prompt and change directories to the project where you want to create the scaffolded items.
- Run the following command updating MyApp.Models.ApplicationDbContext to your own DbContext: dotnet aspnet-codegenerator identity -dc MyApp.Models.ApplicationDbContext
That should fix the “There was an error running the selected code generator” issue. You can also pass a set of parameters into the dotnet aspnet-codegenerator app if you only want to scaffold a certain number of files versus all Identity files.