How can I scaffold all identity files into an ASP.NET Core 2.1 MVC project via dotnet?
Asked Answered
W

3

21

In the MSDN article titled Scaffold Identity in ASP.NET Core projects there is a set of instructions specifically for "creating full identity UI source" (instead of using the Razor Class Library for identity).

This section starts with:

To maintain full control of the Identity UI, run the Identity scaffolder and select Override all files.

There is no command given that can be ran in a shell to scaffold all of these files so I assume "override all files" is a UI control in Visual Studio.

If we look at the output of dotnet aspnet-codegenerator identity -h we will not see any option to generate all files.

Usage: aspnet-codegenerator [arguments] [options]

Arguments:
  generator  Name of the generator. Check available generators below.

Options:
  -p|--project             Path to .csproj file in the project.
  -n|--nuget-package-dir   
  -c|--configuration       Configuration for the project (Possible values: Debug/ Release)
  -tfm|--target-framework  Target Framework to use. (Short folder name of the tfm. eg. net46)
  -b|--build-base-path     
  --no-build               

Selected Code Generator: identity

Generator Options:
  --dbContext|-dc      : Name of the DbContext to use, or generate (if it does not exist).
  --files|-fi          : List of semicolon separated files to scaffold. Use the --list-files option to see the available options.
  --listFiles|-lf      : Lists the files that can be scaffolded by using the '--files' option.
  --userClass|-u       : Name of the User class to generate.
  --useSqLite|-sqlite  : Flag to specify if DbContext should use SQLite instead of SQL Server.
  --force|-f           : Use this option to overwrite existing files.
  --useDefaultUI|-udui : Use this option to setup identity and to use Default UI.
  --layout|-l          : Specify a custom layout file to use.
  --generateLayout|-gl : Use this option to generate a new _Layout.cshtml

Given all of this how can users of the dotnet command-line scaffolding tool generate all of the files that are part of the Identity generator?

Weidman answered 7/9, 2018 at 13:41 Comment(0)
G
20

If you omit the --files and --useDefaultUI flags it will generate all the files.

$ dotnet aspnet-codegenerator identity

Per the docs:

If you run the Identity scaffolder without specifying the --files flag or the --useDefaultUI flag, all the available Identity UI pages will be created in your project.


Sources:

https://github.com/aspnet/Docs/pull/8752

Goody answered 31/1, 2019 at 22:16 Comment(3)
As per the official docs, there are more steps: 1) Install dotnet-aspnet-codegenerator NuGet package globally; 2) Add Microsoft.VisualStudio.Web.CodeGeneration.Design locally; 3) Run dotnet aspnet-codegenerator identity command for creating all files.Ailurophile
How do we generate Razor Views in the /Views/Account folder instead of Razor Pages?Currency
@Currency You may be able to use --relativeFolderPath or -outDir The relative output folder path from project where the file are generated. If not specified, files are generated in the project folder. docsGoody
W
14

As noted there is currently no command-line option to generate all of the identity files.

Thankfully the --files and --listFiles options can be used together to achieve this goal.

Step 1: List the files that can be scaffolded

$ dotnet aspnet-codegenerator identity --listFiles
Building project ...
Finding the generator 'identity'...
Running the generator 'identity'...
File List:
Account.AccessDenied
Account.ConfirmEmail
Account.ExternalLogin
Account.ForgotPassword
Account.ForgotPasswordConfirmation
Account.Lockout
Account.Login
Account.LoginWith2fa
Account.LoginWithRecoveryCode
Account.Logout
Account.Manage._Layout
Account.Manage._ManageNav
Account.Manage._StatusMessage
Account.Manage.ChangePassword
Account.Manage.DeletePersonalData
Account.Manage.Disable2fa
Account.Manage.DownloadPersonalData
Account.Manage.EnableAuthenticator
Account.Manage.ExternalLogins
Account.Manage.GenerateRecoveryCodes
Account.Manage.Index
Account.Manage.PersonalData
Account.Manage.ResetAuthenticator
Account.Manage.SetPassword
Account.Manage.TwoFactorAuthentication
Account.Register
Account.ResetPassword
Account.ResetPasswordConfirmation

We want all of the lines after "File List:".

Step 2: Combine these names into a semi-colon-delimited string

Account.AccessDenied;Account.ConfirmEmail;Account.ExternalLogin;Account.ForgotPassword;Account.ForgotPasswordConfirmation;Account.Lockout;Account.Login;Account.LoginWith2fa;Account.LoginWithRecoveryCode;Account.Logout;Account.Manage._Layout;Account.Manage._ManageNav;Account.Manage._StatusMessage;Account.Manage.ChangePassword;Account.Manage.DeletePersonalData;Account.Manage.Disable2fa;Account.Manage.DownloadPersonalData;Account.Manage.EnableAuthenticator;Account.Manage.ExternalLogins;Account.Manage.GenerateRecoveryCodes;Account.Manage.Index;Account.Manage.PersonalData;Account.Manage.ResetAuthenticator;Account.Manage.SetPassword;Account.Manage.TwoFactorAuthentication;Account.Register;Account.ResetPassword;Account.ResetPasswordConfirmation

Step 3: Run the generator again this time giving the option --files the string we just created

We can't forget to surround with quotes or our shell may attempt to execute these file names as commands (because ; is the command terminator).

$ dotnet aspnet-codegenerator identity --files="Account.AccessDenied;Account.ConfirmEmail;Account.ExternalLogin;Account.ForgotPassword;Account.ForgotPasswordConfirmation;Account.Lockout;Account.Login;Account.LoginWith2fa;Account.LoginWithRecoveryCode;Account.Logout;Account.Manage._Layout;Account.Manage._ManageNav;Account.Manage._StatusMessage;Account.Manage.ChangePassword;Account.Manage.DeletePersonalData;Account.Manage.Disable2fa;Account.Manage.DownloadPersonalData;Account.Manage.EnableAuthenticator;Account.Manage.ExternalLogins;Account.Manage.GenerateRecoveryCodes;Account.Manage.Index;Account.Manage.PersonalData;Account.Manage.ResetAuthenticator;Account.Manage.SetPassword;Account.Manage.TwoFactorAuthentication;Account.Register;Account.ResetPassword;Account.ResetPasswordConfirmation"

Assuming that executed successfully we now have all of the identity code (backend code, UI, etc) directly in our source tree.


References:

https://github.com/aspnet/Docs/issues/8443

https://github.com/aspnet/Scaffolding/issues/872

Weidman answered 7/9, 2018 at 13:41 Comment(2)
Would it not be nice to have an option like --all-files instead of specifying long list of files, just my two cents.Swag
Yes, not specifying files will generate all the files dotnet aspnet-codegenerator identityHocuspocus
S
0

I tried this and it worked (on asp.net core 6.0), but it turned out to not really different than not having a --file value at all. Regardless I thought someone might find it useful

dotnet aspnet-codegenerator identity -dc MyApp.Data.ApplicationDbContext --files "$(dotnet aspnet-codegenerator identity -dc MyApp.Data.ApplicationDbContext --listFiles | grep "Account.*" | tr '\n' ';')"

basically you're taking the command that generates the files, then feeding to it the list of possible files (they all start with 'Account.', thus the grep), with all the new lines from that output translated into semi-colons.

For some reason I thought listFiles would a slightly more specific-to-my-application list but it's not.

Salvadorsalvadore answered 3/3, 2022 at 2:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.