Search code examples
asp.net-mvcasp.net-coremodelasp.net-identityasp.net-core-identity

How do I fully customise the default ASP .Net Core Identity pages?


I'm currently trying to learn and understand all the new Identity bits and pieces with .Net Core 2.1/MVC. Previously, I have used MVC3 MVC Identity and it was straight forward - it automatically created the Model, View and Controllers and I could easily edit everything and anything.

With .Net Core, it seems to automatically inherit everything from the framework and I just don't easily understand how to override.

I've followed the guides located here, and have managed to scaffold all the pages (e.g. create user, reset password, login), however, this does not create any of the models or controllers.

Because of this, I now have a project with all the pages, but, it fails to build with all the models missing:

[Output/Models missing]

CS0246  The type or namespace name 'ChangePasswordModel' could not be found (are you missing a using directive or an assembly reference?

I feel like I am missing something obvious and I can't believe that I am struggling so much on something that ~10 years ago was simple.

How can I create/import the missing Controllers and models?


Solution

  • Identity comes with a default UI composed of Razor Pages and static files housed in a Razor Class Library. It's added by default when you either use AddDefaultIdentity to enable Identity in your project or explicitly call AddDefaultUI on any of the other Identity bootstrapping extensions methods (AddIdentity/AddIdentityCore).

    Views and static files in Razor Class Libraries are embedded resources, and as such are treated as if they physically existed directly in the apps that reference them. However, anything that actually does physically exist in your app will be used before any embedded resources. As such, you can override anything coming from an RCL, simply by creating the same file in the same location as it exists in the RCL in your app. The Identity scaffold simply copies over the selected Razor Pages into your project such that they will override in this way.

    Since Razor Pages are used for the default UI, there's no controllers or separate model classes. The models used for the views are established in the Razor Page codebehinds, so for each page you scaffold in, you should have both Page.cshtml and Page.cshtml.cs files added.

    If your build is failing after doing the scaffold, the build is not seeing the *.cshtml.cs files (codebehinds). Verify that they do exist in your project's file structure. If they don't, there's some fundamental problem with the scaffold in your instance of Visual Studio, which will likely require reinstalling or at least repairing Visual Studio.

    Assuming they are there, then there's something stuck in the build process. Occasionally files do get locked and Visual Studio will complain that it's missing references, when in fact it's simply missing the very assemblies those come from. In such cases, you can navigate into your project directly and remove the bin and obj directories completely. Then, come back into Visual Studio and do a rebuild. If you have more than one project, you should rebuild your entire solution, as it could be a project reference that's actually failing to build.