如何通过dotnet将所有身份文件放入ASP.NET Core 2.1 MVC项目中?

tar*_*ryn 5 code-generation asp.net-core

在MSDN中标题为“ ASP.NET Core项目中的脚手架身份”的文章中有一组专门针对“创建完整身份UI源”(而不是使用Razor类库获取身份)的说明。

本节开始于:

要保持对Identity UI的完全控制,请运行Identity scaffolder,然后选择Override all files。

没有给出可以在外壳中运行的命令以脚手架所有这些文件,因此我认为“覆盖所有文件”是Visual Studio中的UI控件。

如果我们看一下的输出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
Run Code Online (Sandbox Code Playgroud)

考虑到所有这些,dotnet命令行脚手架工具的用户如何生成属于身份生成器的所有文件?

Nat*_*wry 13

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

$ dotnet aspnet-codegenerator identity
Run Code Online (Sandbox Code Playgroud)

根据文档

如果您在未指定--files 标志或--useDefaultUI标志的情况下运行 Identity 脚手架,则将在您的项目中创建所有可用的 Identity UI 页面。


资料来源:

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

  • 根据 [官方文档](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.2&tabs=netcore-cli#scaffold-identity-into -an-empty-project),还有更多步骤: 1) 全局安装 `dotnet-aspnet-codegenerator` NuGet 包;2) 在本地添加`Microsoft.VisualStudio.Web.CodeGeneration.Design`;3) 运行`dotnet aspnet-codegenerator identity`命令来创建所有文件。 (2认同)

tar*_*ryn 7

如上所述,当前没有命令行选项可生成所有身份文件。

值得庆幸的是,--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
Run Code Online (Sandbox Code Playgroud)

我们希望“文件列表:” 之后的所有行。

步骤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
Run Code Online (Sandbox Code Playgroud)

步骤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"
Run Code Online (Sandbox Code Playgroud)

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


参考文献:

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

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