ASP.NET Core 验证:,三ASP.NET Core基于脚手架的Identity

ASP.NET Core 以 Razor Class Library 的形式提供了ASP.NET Core Identity 。包含有Identity的应用程序可应用脚手架来选择性的添加包含在Identity RCL中的代码。你或许想要生成代码以便对它们进行改动并改变它们的行为。举个例例子,你可以指示脚手架来生成用在注册中的代码。生成的代码优先于Identity RCL中的代码。为了获取对UI的完全控制并不使用默认的RCL,请参考章节 Create full identity UI source 的内容。

没有使用验证的应用程序可以应用脚手架来添加 RCL Identity 包。你有一个选项来决定是否需要生成相关的Identity 代码。

虽然脚手架生成了大部分必要的代码,然而你仍然需要更新你的工程来完成这一过程。这篇文章解释了完成Identity 脚手架更新所需的步骤。

我们建议你使用一个源代码控制系统,其可以用来显示更改并允许你撤回进行的更改。并在允许Identity脚手架之后可以查看所添加的更改。

当和Identity一起使用Two Factor AuthenticationAccount confirmation 和 password recovery以及其他安全特性时我们需要一些服务。而当我们使用脚手架来生成Identity时服务以及服务的凭证并不会被生成。启用这些特性的服务必须被手动添加。有个例子,可以参考Require Email Confirmation

当以一个新的数据上下文来给一个已经存在个人账户的工程生成脚手架Identity时:在 Startup.ConfigureServices 中,你需要移除对如下列表的调用:

  • AddDbContext
  • AddDefaultIdentity

举个例子,在如下的代码中,我们屏蔽了AddDbContext 以及AddDefaultIdentity:

public void ConfigureServices(IServiceCollection services)
{
    //services.AddDbContext<ApplicationDbContext>(options =>
    //    options.UseSqlServer(
    //        Configuration.GetConnectionString("DefaultConnection")));
    //services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    //    .AddEntityFrameworkStores<ApplicationDbContext>();
    services.AddControllersWithViews();
    services.AddRazorPages();
}

上述代码注释掉了与Areas/Identity/IdentityHostingStartup.cs 重复的代码。

典型的,以个人账户创建的app不应该创建一个新的数据上下文。

给一个空工程基于脚手架生成Identity

运行Identity脚手架:

  • 在解决方案资源管理器中,右键 项目>>添加>>新搭建基于基架的项目。
  • 从添加基架对话框的左侧面板中,选择 Identity,点击添加。
  • 在添加Identity对话框中,勾选你想要添加的选项。选择你的已经存在的布局页,否则你的布局页面会被不正确的标记重写。比如Razor Pages工程就是~/Pages/Shared/_Layout.cshtml,MVC工程就是~/Views/Shared/_Layout.cshtml。
  • 选择 “+” 按钮来创建一个新的数据上下文类。
  • 点击 添加。

使用与如下类似的代码来更新Startup 类:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddControllersWithViews();
        services.AddRazorPages();
    }

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

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });
    }
}

UseHsts 是推荐的但不是必须的。查看HTTP Strict Transport Security Protocol 以获取更多的信息。

生成的Identity 数据库代码需要Entity Framework Core Migrations。创建一个迁移文件并更新数据库。举个例子,运行如下命令:

Install-Package Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
Add-Migration CreateIdentitySchema
Update-Database

Add-Migraton的名称参数"CreateIdentitySchema" 是任意的。这个名字"CreateIdentitySchema" 描述了迁移文件。

用脚手架给一个不具有验证的Razor工程添加Identity

运行Identity脚手架:

  • 在解决方案资源管理器中,右键 项目>>添加>>新搭建基于基架的项目。
  • 从添加基架对话框的左侧面板中,选择 Identity,点击添加。
  • 在添加Identity对话框中,勾选你想要添加的选项。选择你的已经存在的布局页,否则你的布局页面会被不正确的标记重写。比如Razor Pages工程就是~/Pages/Shared/_Layout.cshtml,MVC工程就是~/Views/Shared/_Layout.cshtml。
  • 选择 “+” 按钮来创建一个新的数据上下文类。
  • 点击 添加。

TO BE CONTIUED...