241

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

Areas file structure

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?

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
Bob.at.Indigo.Health
  • 11,023
  • 13
  • 64
  • 111

7 Answers7

242

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://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio

Joe Audette
  • 35,330
  • 11
  • 106
  • 99
  • 226
    Thumbs up for the answer, and thumbs down for the Microsoft. What is the point of hiding code from developers, and giving them a basic UI to use for Auth? That's so funny. Come on Microsoft, we wanna look into code, learn it, break it, fix it, and deploy it. This is odd. – Teoman shipahi Oct 15 '18 at 03:32
  • 4
    is there any PM command so we can get all identity files to local . – panky sharma Dec 07 '18 at 07:54
  • 4
    @pankysharma I just added an answer with a command for doing that (in case anyone in the future needs it). – Alisson Reinaldo Silva Aug 05 '19 at 02:34
  • 4
    Do we seriously have to use Razor pages? Is there no option to simply scaffold regular MVC views? – clockwiseq Sep 16 '19 at 21:40
  • If anyone else is dumb like me... Took me way longer than it should to realize that you can view the Get/Post code by right-clicking the file and selecting "Go To Page Model" – jamesSampica Nov 01 '19 at 12:51
  • 1
    I agree so much with @Teomanshipahi, I spent a whole day trying to fix this mess, all because some code is hidden and you're never sure what's really happening. – Filipe Madureira Dec 03 '19 at 08:50
  • no kidding... we use the user accounts and not the email addresses for logging in... did not realize we all had to fall into a line – hal9000 Feb 24 '20 at 18:12
  • As an excersise to learn MVC Core I'm writing a basic application. I decided to make it localisable from the start and found some useful stuff online that I copied so strings were looked up from resource files. I decided to use Identity rather than write my own user management and discovered that all the strings are hardcoded in English and I'd need to scaffold all the pages and replace them all! That's nuts. Come on Microsoft. How hard would it be to have a string lookup mechanism baked in? – Ian Sep 18 '21 at 15:31
  • I am not a developer and this answer helped me to understand how the project works. Thank you very much!! – FranciscoNabas Nov 19 '21 at 18:10
124

You can do this from within Visual Studio, Right click on your Project and select Add->New Scaffolded Item

New Scaffolded Item

Then select Identity and click on Add Select Identity

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.

Select Pages

Finally click on Add. If you have already existing override files there (i.e. from the template) this will warn about overwriting them.

John Rah
  • 1,757
  • 1
  • 14
  • 13
  • This does in fact work. However, my code would not compile cleanly after I had imported the Login page - the page I am interested in customizing. The solution can be found here: https://stackoverflow.com/questions/58277172/how-to-modify-asp-net-identity-ui-for-asp-net-core-webapi-with-angular. – Yossi G. Oct 31 '19 at 04:54
  • Thankyou for the great answer John, Any idea why User Class Dropdown (at the end of form) is disabled? – Hamza Khanzada Nov 30 '19 at 15:51
  • 2
    Thanks it did the job – AhmadMM Dec 11 '19 at 13:24
  • From memory, the User Class becomes enabled once you create a Data context class (click the + button) – John Rah Dec 13 '19 at 07:03
  • 2
    Best answer here. – Benj Sanders Nov 02 '20 at 22:33
  • Here is an article from MS on how to scaffold Identity into the various frameworks... https://learn.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-5.0&tabs=visual-studio – Chris Catignani Apr 16 '21 at 17:43
45

Right-click on the project, select Add -> New Scaffold Item, on the dialog select Identity in the left pane.

enter image description here

enter image description here


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:

enter image description 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" />
Alisson Reinaldo Silva
  • 10,009
  • 5
  • 65
  • 83
7

'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.

GioLopera
  • 81
  • 1
  • 3
  • 1
    how to change to view or edit identity code on localhost – panky sharma Dec 07 '18 at 07:57
  • I've used this today in an existing project in which I updated to 2.2, with existing DbContext and it worked awesome once I'd told my existing context it was inheriting from Identity! – jamheadart Aug 13 '19 at 17:34
2

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.

Will Hunt
  • 39
  • 1
2

Once I found this answer my next question was where is the controller code for identity views? And I found is all csharp codes are attached with these .cshtml files now. I added picture for better understanding.

How to find csharp code of those views

Liakat Hossain
  • 1,288
  • 1
  • 13
  • 24
0

Just wanted to add. If you are looking for the underlying classes for these pages, just open up the related page folder. Example, for the Register class, open up the Register.cshmtl to find the underlying Register.cshmtl.cs file. Please see below: enter image description here

SimpleUser
  • 1,341
  • 2
  • 16
  • 36