如何在Visual Studio for Mac 2022(ASP.Net 6 MVC)中生成Identity页面?
在Visual Studio for Mac 2022中生成ASP.NET 6 MVC Identity页面的方法
可行,只是Visual Studio for Mac的操作路径和Windows版本不同,或者可以直接使用跨平台的.NET CLI工具完成,以下是具体步骤:
方法一:使用.NET CLI(推荐,跨平台可靠)
- 安装脚手架依赖包
打开终端,进入项目根目录,运行:dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design - 安装Identity代码生成工具(全局安装,仅需一次)
dotnet tool install -g dotnet-aspnet-codegenerator - 生成指定的Identity页面
替换命令中的YourDbContextClass为项目实际的DbContext类名,可按需指定要生成的页面文件:
如果需要生成所有Identity页面,去掉dotnet aspnet-codegenerator identity -dc YourDbContextClass --files Account.Register,Account.Login,Account.Logout,Manage.Index--files参数即可。 - 验证项目配置
检查Program.cs(ASP.NET 6采用顶级语句)中的服务注册和中间件配置:- 确保已注册Identity服务:
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<YourDbContextClass>(); - 确保中间件顺序正确:
app.UseAuthentication(); app.UseAuthorization();
- 确保已注册Identity服务:
方法二:尝试Visual Studio for Mac内置脚手架
- 右键点击项目,选择
Add->New Scaffolded Item... - 若未看到
Identity选项,先安装步骤一中的Microsoft.VisualStudio.Web.CodeGeneration.Design包,重启VS Mac后再尝试。 - 若仍无该选项,建议直接使用CLI方法——VS Mac的Identity脚手架支持相对Windows版本不够完善,但CLI工具是官方原生支持的跨平台方案。
注意事项
- 生成前确保项目已正确配置DbContext,且Identity实体(如
IdentityUser)已设置完成。 - 若项目已有身份验证代码,生成时建议指定具体页面文件,避免覆盖现有逻辑。
内容的提问来源于stack exchange,提问作者Josh Livingstone
相关产品推荐
相关产品推荐

