如何从ASP.NET Identity Core 2.0中删除与角色相关的表 [英] How to remove the role-related tables from ASP.NET Identity Core 2.0

查看:109
本文介绍了如何从ASP.NET Identity Core 2.0中删除与角色相关的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他建议中,角色是索赔的子集,我正在寻找一种干净的方法,要求ASP.NET Identity中的EF Core实现不要在ASP.NET Identity Core 2.0中创建与角色相关的表VS 2017中的模板.仅要求声明. 模板使用

With the advice read-elsewhere that Roles are a subset of Claims, I am looking at a clean way to ask the EF Core implementation in ASP.NET Identity not to create role-related tables in the ASP.NET Identity Core 2.0 template in VS 2017. Only claims are needed. The template uses

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

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

然后IdentityDbContext创建这些与角色相关的表

And IdentityDbContext creates these Roles-related tables

https://github .com/aspnet/Identity/blob/dev/src/Microsoft.AspNetCore.Identity.EntityFrameworkCore/IdentityDbContext.cs

如何在不处理迁移文件的情况下摆脱它们?

How to get rid of them without manipulating the migration files?

推荐答案

在ASP.NET身份中始终可以做到这一点,但随着时间的推移,这种惯例变得越来越容易,因为惯例已从角色转变为权利,行动,主张,谓词以及其他更可重用和可维护的语义.我在ASP.NET项目中使用已有的DB模式(没有角色表)已经使用多年了.我承认,由于ASP.NET Identity的繁琐复杂性,以及ASP.NET中发生的快节奏的开源代码更改以及完全缺乏人工指导的文档,很难理解如何执行此操作在API参考中(完全由机器生成的样板文件).

This has always been possible in ASP.NET Identity, but has become easier over time, as conventions have moved away from Roles and towards Rights, Actions, Claims, Predicates, and other more reusable and maintainable semantics. I've been using Identity for years in my ASP.NET projects with my pre-existing DB schemas (which have no role tables). I admit, it's quite difficult to understand how to do this due to the cumbersome complexity of ASP.NET Identity, as well as the fast-paced open-source code changes occurring in ASP.NET coupled with the complete lack of human-informed documentation in the API reference (which is entirely boilerplate machine-generated).

在ASP.NET Core之前,您可以通过覆盖UserManagerUserStore实现来实现.通过不使用操作取消角色请求,或使用更有用且对开发人员更安全的实现(可能不是基于魔术字符串!)覆盖RoleAttribute实现,就不会引起对角色表的注意.即使使用默认的实现,如果您从未使用默认的Role属性实现或询问的Role问题,则可以删除表而不会产生任何后果.默认的ASP.NET脚手架都不取决于角色.

Prior to ASP.NET Core, you could do this by overriding UserManager and UserStore implementations. By stubbing out the Role requests with no-ops, or overriding the RoleAttribute implementation with a more useful and developer-safe one (probably not based on magic strings!), the absence of the Role tables goes unnoticed. Even using the default implementations, if you never used default Role attribute implementations or asked Role questions, the tables could be removed without consequence. None of the default ASP.NET scaffolding depends on roles.

在原始ASP.NET Core Identity 1.0/1.1版本中,您是通过实现UserStore而不使用可选的IUserRoleStore接口来完成此操作的.有关此信息,请参见

In original ASP.NET Core Identity 1.0/1.1 versions, you did this by implementing UserStore without the optional IUserRoleStore interface. Information on that can be found in the main ASP.NET Core Identity documentation.

从ASP.NET Core 2.0开始(根据您的主要问题),您可以通过从IdentityUserContext而不是IdentityDbContext派生上下文来更轻松地执行此操作,如以下示例所示.由于 AddIdentityCore .如果您依赖其他标准身份验证功能,AddIdentityCore需要几行代码,因为默认情况下,它不初始化Cookies或TokenProviders. (如下所述,在2.1中,不再需要对样板Startup进行更改.)

As of ASP.NET Core 2.0 (per your main question), you could do this more easily by deriving your context from IdentityUserContext instead of IdentityDbContext, as per the example below. This no longer required custom implementations in 2.0 due to the new UserOnlyStore. The call to AddIdentity in Startup.cs also needed to be replaced with AddIdentityCore. AddIdentityCore requires a few extra lines of code if you depend on other standard Authentication features, as by default it does not initialize Cookies or TokenProviders. (As noted below, in 2.1, changes to the boilerplate Startup are no longer required.)

删除ASP.NET Core 2.1/2.2中的角色非常简单(在撰写本文时,该角色是最新的).这是一个使用新项目进行演示的示例:

It's quite simple to remove roles in ASP.NET Core 2.1/2.2 (current as of this writing). Here's an example using a new project to demonstrate:

  1. 创建一个新项目以演示身份,选择:

  1. Create a new project to demonstrate identity, selecting:

  1. ASP.NET Core Web应用程序项目类型
  2. Web应用程序(两种类型,例如MVC)
  3. 更改身份验证
  4. 个人用户帐户
  5. 在应用内存储用户帐户

  • 从新搭建的Identity项目中删除角色

  • Remove Roles from the newly scaffolded Identity project

    1. 编辑Data \ ApplicationDbContext.cs,将上下文基类提升到角色上方
      • 来自:ApplicationDbContext : IdentityDbContext
      • 至:ApplicationDbContext : IdentityUserContext<IdentityUser>
    1. edit Data\ApplicationDbContext.cs, elevating the context base class above roles
      • from: ApplicationDbContext : IdentityDbContext
      • to: ApplicationDbContext : IdentityUserContext<IdentityUser>

  • 请注意IdentityUserContext缺少角色,因此自定义键类型仅需要2个参数

  • Note the IdentityUserContext lacks Role, so custom key types only require 2 arguments

    1. 在ApplicationDbContext.cs中:IdentityUserContext<IdentityUser<int>, int>
    2. 在Startup.cs中,AddDefaultIdentity<IdentityUser<int>>()被指定为以前的
    3. 提供给_LoginPartial.cshtml的模型也像以前一样指定. 有关更改身份模型的更多详细信息
    4. 如果您更改了身份密钥类型,则默认的EF迁移过程将失败
    1. in ApplicationDbContext.cs: IdentityUserContext<IdentityUser<int>, int>
    2. in Startup.cs, AddDefaultIdentity<IdentityUser<int>>() is specified as before
    3. The model provided to the _LoginPartial.cshtml is also specified as before. more details on changing Identity models
    4. if you've changed identity key type, the default EF Migration process fails
    1. 如果您更改了密钥,EF会生成非功能性迁移
    2. 只需删除Data \ Migrations即可在测试中工作,但需要注意以下几点:
      • 脚手架项目中包含的索引不是默认值
      • 如果您已经运行了项目,则需要删除数据库
    1. EF generates non-functional migrations if you've changed keys
    2. simply deleting Data\Migrations works in test, with these caveats:
      • the scaffolded project included indices that are not default
      • if you've already run the project, you'll need to delete the DB

  • 更新/构建数据库架构以反映上述内容.在程序包管理器控制台中:

  • Update/build the database schema to reflect the above. In the Package Manager Console:

    1. Add-Migration RemoveIdentitySchemaRoles
    2. Update-Database
    1. Add-Migration RemoveIdentitySchemaRoles
    2. Update-Database

  • 运行应用程序

  • Run the app

    这篇关于如何从ASP.NET Identity Core 2.0中删除与角色相关的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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