Where are the Login and Register pages in an AspNet Core scaffolded app?
asp.net Coreasp.net IdentityRazor Pagesasp.net Core Problem Overview
In VS 2017, I created a new ASP.NET Core Web Application. On the second page of the wizard, I chose Web Application, and for Authentication, I chose "Individual User Accounts".
Now, I'm trying to find the Pages associated with /Account/Register and /Account/Login.
_Layout.cshtml brings in _LoginPartial.cshtml, much as it did in classic MVC:
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-page="/Index">Home</a></li>
<li><a asp-page="/About">About</a></li>
<li><a asp-page="/Contact">Contact</a></li>
</ul>
<partial name="_LoginPartial" />
</div>
If the user is not signed in then _LoginPartial includes <a>
tags that point to the login and register pages:
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="Identity" asp-page="/Account/Register">Register</a></li>
<li><a asp-area="Identity" asp-page="/Account/Login">Login</a></li>
</ul>
That all seems to make sense. But I would have expected the Areas folder structure to include the Register and Login folders. It does not. The only thing I find there is _ViewStart.cshtml
I know that the scaffolded code works. When I run the project, the Register link points to "/Identity/Account/Register", and the Login link points to "/Identity/Account/Login". Clicking on the Register link gets me a registration page that includes the text "Create a new account".
But I can't find the text "Create a new account" anywhere in the project.
Can someone tell me what I'm missing?
asp.net Core Solutions
Solution 1 - asp.net Core
It was announced during the preview of asp.net core 2.1 that the Identity UI would be moved to a new Razor Class Library. https://blogs.msdn.microsoft.com/webdev/2018/03/02/aspnetcore-2-1-identity-ui/
It is still possible to scaffold the Identity Views into your own project if you prefer local views: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio
Solution 2 - asp.net Core
You can do this from within Visual Studio, Right click on your Project and select Add->New Scaffolded Item
Then select Identity and click on Add
Now select the pages you want to override. Drop down Data Context Class: and your Namespace and Context should be pre-filled in for you.
Finally click on Add. If you have already existing override files there (i.e. from the template) this will warn about overwriting them.
Solution 3 - asp.net Core
Right-click on the project, select Add -> New Scaffold Item
, on the dialog select Identity in the left pane.
You can also generate these pages using this command:
dotnet aspnet-codegenerator identity -dc WebApplication1.Data.ApplicationDbContext
Make sure you replace with your own namespace + DbContext name. The pages are generated here:
If you see this problem:
> Feature 'default literal' is not available in C# 7.0. Please use > language version 7.1 or greater.
You may be able to fix it by adding this to you .csproj
and rebuilding it:
<PackageReference Include="Microsoft.CodeAnalysis.Common" Version="3.2.0-beta3-final" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.2.0-beta3-final" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.2.0-beta3-final" />
Solution 4 - asp.net Core
'If you want all the identity code to be in your application so that you can change it however you want, you can use the new identity scaffolder to add the identity code to your application. All the scaffolded identity code is generated in an identity specific area folder so that it remains nicely separated from your application code.'
https://blogs.msdn.microsoft.com/webdev/2018/02/02/asp-net-core-2-1-roadmap/#identity
It works pretty nice and easy for me.
Solution 5 - asp.net Core
Little late to the party but after frustrations with the Identity UI I gave up on it. Create your projects without it. My advice is (when using 2.1 or 2.2) create your own account/manage controllers and mvc pages. Some startup.cs debugging will be necessary. Also disable the publish view (i.e. views.dll) compilation option. Maybe I don't have an option set but the compiler completely ignores any changes in page level script blocks.