带有 AppplicationDbContext 的身份脚手架 [英] Identity scaffolding with AppplicationDbContext

查看:39
本文介绍了带有 AppplicationDbContext 的身份脚手架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET Core 2.x 现在隐藏了库中的标识页面和逻辑.为了使用新属性扩展 IdentityUser 实体并添加 UI 和逻辑来处理这些新属性,我们现在开始运行身份脚手架"(在 MyProject -> Add -> New scaffolded item).不幸的是,文档 充其量是不完整的,并且在很多情况下,完全错误.

ASP.NET Core 2.x now hides the Identity pages and logic inside of a library. In order to extend the IdentityUser entity with new properties and add UI and logic to deal with those new properties, we now get to run the "Identity scaffolder" (under MyProject -> Add -> New scaffolded item). Unfortunately, the docs are incomplete at best and, in more than a few instance, flat out wrong.

我正在努力弄清楚如何解决脚手架和生成的代码来实现我认为是一个非常标准的用例:

I'm struggling to figure out how to wrestle the scaffolder and the resultant code to achieve what I would assume is a pretty standard use case:

  1. 我从一个新的 ASP.NET Core MVC 应用开始
  2. 我想用我自己的属性扩展 IdentityUser.由于这涉及创建一个继承类,我希望该新类的名称为 ApplicationUser.
  3. 我想搭建需要处理的身份页面(登录、注册、管理等)
  4. 我想使用现有的ApplicationDbContext
  1. I'm starting with a new ASP.NET Core MVC app
  2. I want to extend IdentityUser with my own properties. Since this involves creating an inherited class, I want the name of that new class to be ApplicationUser.
  3. I want to scaffold the Identity pages I need to fiddle with (Login, Register, Manage, etc.)
  4. I want to use the existing ApplicationDbContext

我的问题是,阅读文档并使用身份脚手架时,我不知道如何让脚手架让我扩展 IdentityUser 使用现有的 ApplicationDbContext.

My problem is that, reading the docs and playing with the Identity scaffolder, I can't figure out how to get the scaffolder to let me extend IdentityUser and keep using the existing ApplicationDbContext.

推荐答案

盯着文档 并使用脚手架和生成的代码,我想我已经弄清楚了大部分.

After staring at the docs and playing with the scaffolder and the resultant code, I think I've figure out most of it.

  1. 从新的 ASP.NET Core Web 应用程序开始,使用个人用户帐户"身份验证.显然,从Web 应用程序"(即 Razor Pages)还是Web 应用程序(模型-视图-控制器)"(即 MVC)开始并不重要.最终,身份页面将是 Razor 页面,但它们在 MVC 应用程序中运行良好.
  2. 构建解决方案.我不确定这是否有必要,但它不会伤害...
  3. 在继续之前,确保您喜欢您的数据库连接字符串(在 appsettings.json 中).在包管理器控制台中,输入命令 update-database.这显然更新了 ApplicationDbContextModelSnapshot.cs 文件以包含您从项目模板中获得的初始迁移.如果您跳过此步骤,那么当您添加第一个迁移时,它将包括您在 00000000000000_CreateIdentitySchema.cs 中已有的所有迁移步骤.此外,显然需要此步骤才能让脚手架识别现有的 ApplicationDbContext.
  4. 右键单击项目并选择添加 -> 新建脚手架项;选择身份项目类型
  5. 指定您现有的 _Layout.cshtml 并选择您想要搭建的页面
  6. 在数据上下文类旁边,单击下拉箭头并选择现有的 ApplicationDbContext
  7. 单击添加"按钮以执行脚手架.
  1. Start with a new ASP.NET Core Web Application, with "Individual user accounts" authentication. Apparently, it doesn't really matter whether you start with a "Web Application" (i.e. Razor Pages) or "Web Application (Model-View-Controller)" (i.e. MVC). In the end, the Identity pages will be Razor Pages, but they function just fine within an MVC app.
  2. Build the solution. I'm not sure if this is necessary, but it doesn't hurt...
  3. Before you go any farther, make sure you like your DB connection string (in appsettings.json). In the Package Manager Console, enter the command update-database. This apparently updates the ApplicationDbContextModelSnapshot.cs file to include the initial migration that you got from the project template. If you skip this step then, when you add your first migration, it will include all of the migration steps you already have in 00000000000000_CreateIdentitySchema.cs. Also, this step is apparently needed in order for the scaffolder to recognize the existing ApplicationDbContext.
  4. Right-click the project and select Add -> New scaffolded item; Select the Identity item type
  5. Specify your existing _Layout.cshtml and select the pages you want to scaffold
  6. Next to Data context class, click the drop-down arrow and select the existing ApplicationDbContext
  7. Click the Add button to perform the scaffolding.

此时,您的项目已连接到现有的 ApplicationDbContextIdentityUser 类.现在,您可以扩展 IdentityUser 并连接身份页面以使用新的扩展实体:

At this point, your project is wired up to the existing ApplicationDbContext and IdentityUser classes. Now, you can extend IdentityUser and wire up the Identity pages to use the new, extended entity:

添加一个从 IdentityUser 继承的新 ApplicationUser 类:

Add a new ApplicationUser class that inherits from IdentityUser:

public class ApplicationUser : IdentityUser
{
    public string Nickname { get; set; }
}

将新的 ApplicationUser 实体添加到 ApplicationDbContext 类:

Add the new ApplicationUser entity to the ApplicationDbContext class:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<ApplicationUser> AppUsers { get; set; }
}

的所有实例替换为 .这意味着您将更改 startup.cs 以调用 services.AddDefaultIdentity(),并且您将替换对 SigninManagerUserManager<的所有引用/code> 指定新的 类型参数.

Replace all instances of <IdentityUser> with <ApplicationUser>. This means that you will change startup.cs to call services.AddDefaultIdentity<ApplicationUser>(), and you will replace all of the references to SigninManager and UserManager to specify the new <ApplicationUser> Type parameter.

哇...现在,您可以进入诸如 Register.cshtml.cs 之类的内容并创建(或获取)一个新的 ApplicationUser 而不是 IdentityUser,以及所有管道将连接到 ApplicationUser.例如,Register.cshtml.cs 中的代码可能如下所示:

Whew... now, you can go into things like Register.cshtml.cs and create (or fetch) a new ApplicationUser rather than an IdentityUser, and all of the plumbing will be wired up to ApplicationUser. For example, the code in Register.cshtml.cs might look like this:

var user = new ApplicationUser {
    UserName = Input.Email,
    Email = Input.Email,
    Nickname = Input.Nickname // New property
};
var result = await _userManager.CreateAsync(user, Input.Password);

这篇关于带有 AppplicationDbContext 的身份脚手架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆