gpt4 book ai didi

asp.net-mvc - 如何将 ASP.NET Core 标识添加到现有的 Core mvc 项目?

转载 作者:行者123 更新时间:2023-12-04 03:03:14 25 4
gpt4 key购买 nike

我已经使用 CLI 在 Mac 上启动了没有身份的 dotnet core mvc 项目,现在我想添加此功能。到目前为止我所知道的唯一选择是通过以下方式创建一个新项目

dotnet new mvc --auth

有没有更好的方法来为现有项目添加身份?我希望有一个“dotnet new”命令。

最佳答案

根据 docs.microsoft.com您可以使用 将身份搭建到现有的 MVC 项目中aspnet-codegenerator .

1) 如果之前没有安装过 ASP.NET Core 脚手架,现在安装:

dotnet tool install -g dotnet-aspnet-codegenerator

2) 向项目 (*.csproj) 文件中添加对 Microsoft.VisualStudio.Web.CodeGeneration.Design 的包引用。在项目目录中运行以下命令:
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet restore

3) 运行以下命令列出身份脚手架选项:
dotnet aspnet-codegenerator identity -h

4) 在项目文件夹中,使用您想要的选项运行 Identity scaffolder。例如,要使用默认 UI 和最少文件数设置身份,请运行以下命令:
dotnet aspnet-codegenerator identity --useDefaultUI

5) 生成的 Identity 数据库代码需要 Entity Framework Core Migrations。创建迁移并更新数据库。例如,运行以下命令:
dotnet ef migrations add CreateIdentitySchema
dotnet ef database update

6) 调用 使用身份验证 使用静态文件 :
public class Startup
{

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAuthentication(); // <-- add this line
app.UseMvcWithDefaultRoute();
}
}

关于asp.net-mvc - 如何将 ASP.NET Core 标识添加到现有的 Core mvc 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50179696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com