You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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(推荐,跨平台可靠)

  1. 安装脚手架依赖包
    打开终端,进入项目根目录,运行:
    dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
    
  2. 安装Identity代码生成工具(全局安装,仅需一次)
    dotnet tool install -g dotnet-aspnet-codegenerator
    
  3. 生成指定的Identity页面
    替换命令中的YourDbContextClass为项目实际的DbContext类名,可按需指定要生成的页面文件:
    dotnet aspnet-codegenerator identity -dc YourDbContextClass --files Account.Register,Account.Login,Account.Logout,Manage.Index
    
    如果需要生成所有Identity页面,去掉--files参数即可。
  4. 验证项目配置
    检查Program.cs(ASP.NET 6采用顶级语句)中的服务注册和中间件配置:
    • 确保已注册Identity服务:
      builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                      .AddEntityFrameworkStores<YourDbContextClass>();
      
    • 确保中间件顺序正确:
      app.UseAuthentication();
      app.UseAuthorization();
      

方法二:尝试Visual Studio for Mac内置脚手架

  1. 右键点击项目,选择Add -> New Scaffolded Item...
  2. 若未看到Identity选项,先安装步骤一中的Microsoft.VisualStudio.Web.CodeGeneration.Design包,重启VS Mac后再尝试。
  3. 若仍无该选项,建议直接使用CLI方法——VS Mac的Identity脚手架支持相对Windows版本不够完善,但CLI工具是官方原生支持的跨平台方案。

注意事项

  • 生成前确保项目已正确配置DbContext,且Identity实体(如IdentityUser)已设置完成。
  • 若项目已有身份验证代码,生成时建议指定具体页面文件,避免覆盖现有逻辑。

内容的提问来源于stack exchange,提问作者Josh Livingstone

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 11:10:11