如何通过 dotnet 将所有标识文件脚手架到 ASP.NET Core 2.1 MVC 项目中?

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

在标题为 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) 的 MSDN 文章中。

本节开头为:

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

shell 中没有给定的命令可以 运行 来构建所有这些文件,所以我假设 "override all files" 是 UI 中的 [=] 控件33=].

如果我们查看 dotnet aspnet-codegenerator identity -h 的输出,我们 将不会 看到任何生成所有文件的选项。

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

考虑到所有这些,dotnet command-line 脚手架工具的用户如何生成 所有 作为身份生成器一部分的文件?

如前所述,目前没有生成所有身份文件的命令行选项。

谢天谢地,--files--listFiles 选项可以一起使用来实现这个目标。

第 1 步:列出可以搭建的文件

$ 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

我们想要 "File List:".

之后的所有行

第 2 步:将这些名称组合成一个以分号分隔的字符串

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

第 3 步:运行 生成器再次提供选项 --files 我们刚刚创建的字符串

我们不能忘记用引号括起来或者我们的shell可能会尝试将这些文件名作为命令执行(因为;是命令终结者)。

$ 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"

假设执行成功,我们现在在源代码树中直接拥有所有身份代码(后端代码、UI 等)。


参考文献:

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

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

如果省略 --files--useDefaultUI 标志,它将生成所有文件。

$ dotnet aspnet-codegenerator identity

根据 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.


来源:

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

我尝试了这个并且它有效(在 asp.net 核心 6.0 上),但事实证明它与根本没有 --file 值并没有什么不同。不管怎样,我认为有人可能会觉得它有用

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

基本上你正在执行生成文件的命令,然后向它提供可能文件的列表(它们都以 'Account.' 开头,因此是 grep),以及该输出中的所有新行翻译成semi-colons.

出于某种原因,我认为 listFiles 会稍微多一些 specific-to-my-application 列表,但事实并非如此。