Asp.net-Core

如何通過 dotnet 將所有身份文件建構到 ASP.NET Core 2.1 MVC 項目中?

  • March 3, 2022

*在題為ASP.NET Core 項目中的 Scaffold Identity*的 MSDN 文章中,有一組專門用於“創建完整標識 UI 源”的說明(而不是使用Razor 類庫進行標識)。

本節開始於:

要保持對 Identity UI 的完全控制,請執行 Identity 腳手架並選擇 Override all files。

沒有給出可以在 shell 中執行來建構所有這些文件的命令,因此我假設“覆蓋所有文件”是 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

dotnet鑑於所有這些,命令行腳手架工具的使用者如何生成屬於身份生成器的所有文件?

如果省略--filesand--useDefaultUI標誌,它將生成所有文件。

$ dotnet aspnet-codegenerator identity

根據文件

如果您在未指定--files 標誌或--useDefaultUI標誌的情況下執行 Identity 腳手架,則將在您的項目中創建所有可用的 Identity UI 頁面。


資料來源:

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

如前所述,目前沒有用於生成所有身份文件的命令行選項。

值得慶幸的是,--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

我們想要“文件列表:”之後的所有行。

第 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

引用自:https://stackoverflow.com/questions/52223806