How to remove the role-related tables from ASP.NET Identity Core 2.0

Octopus

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);
    }
}

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?

shannon

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).

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.

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.

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.)

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. Create a new project to demonstrate identity, selecting:

    1. ASP.NET Core Web Application project type
    2. Web Application (either type, e.g. MVC)
    3. Change Authentication
    4. Individual User Accounts
    5. Store user accounts in-app
  2. Remove Roles from the newly scaffolded Identity project

    1. edit Data\ApplicationDbContext.cs, elevating the context base class above roles
      • from: ApplicationDbContext : IdentityDbContext
      • to: ApplicationDbContext : IdentityUserContext<IdentityUser>
    2. note that IdentityUserContext requires an IdentityUser generic
    3. due to new Identity code in ASP.NET Core 2.1, that's all that's required
  3. Note the IdentityUserContext lacks Role, so custom key types only require 2 arguments

    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 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
  4. Update/build the database schema to reflect the above. In the Package Manager Console:

    1. Add-Migration RemoveIdentitySchemaRoles
    2. Update-Database
  5. Run the app

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Customizing ASP.NET Core Identity Tables

分類Dev

Role based authorization in ASP.NET Core 3.1 with Identity and ExternalLogin

分類Dev

How to remove an Azure role assignment from a managed identity?

分類Dev

ASP.NET Identity 2 initializer drop all my tables

分類Dev

asp.net identity with custom role

分類Dev

How to implement permission based authorization in ASP.net core Identity?

分類Dev

Creating Role that type of key be integer in ASP.NET Identity

分類Dev

ASP.NET Core MVC MySQL Identity

分類Dev

Asp.net core create interface for identity

分類Dev

How do I connect to DB2 from a docker container using ASP.Net Core?

分類Dev

How to get data from 2 (or more) independent dropdown asp.net core mvc?

分類Dev

Migrating Identity in .net core 2 class library

分類Dev

How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

分類Dev

How do you disable HTTPS in ASP.NET Core project with Identity?

分類Dev

How do you disable HTTPS in ASP.NET Core project with Identity?

分類Dev

use existing login table with asp.net core identity service

分類Dev

How to correctly download files to Angular from ASP.NET Core?

分類Dev

How to get rolename from Roles object in asp.net core

分類Dev

How to Export CSV file from ASP.NET core

分類Dev

ASP.NET Core - How to get parameter from url

分類Dev

ASP.NET Core - Cannot convert from System.security.claims.claimsPrincipal to Scheduler.Areas.Identity.Data

分類Dev

In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role?

分類Dev

Remove route from RouteCollection in Asp.Net Core and add new with same route name (nopCommerce-4.00)

分類Dev

Asp.net MVC 5, Identity 2.0 Unable to add a Role to user

分類Dev

How does Asp.Net Identity 2 User Info get mapped to IdentityServer3 profile claims

分類Dev

ASP.NET Identity2 - How to get User Id with AllowAnonymous Controller?

分類Dev

Angular / .net core 3.1 Get Role Claim from JWT

分類Dev

Can't access method (Azure Tables with ASP.NET Core)

分類Dev

How to I access the DbContext of EF core from another project when used in ASP.NET core?

Related 関連記事

  1. 1

    Customizing ASP.NET Core Identity Tables

  2. 2

    Role based authorization in ASP.NET Core 3.1 with Identity and ExternalLogin

  3. 3

    How to remove an Azure role assignment from a managed identity?

  4. 4

    ASP.NET Identity 2 initializer drop all my tables

  5. 5

    asp.net identity with custom role

  6. 6

    How to implement permission based authorization in ASP.net core Identity?

  7. 7

    Creating Role that type of key be integer in ASP.NET Identity

  8. 8

    ASP.NET Core MVC MySQL Identity

  9. 9

    Asp.net core create interface for identity

  10. 10

    How do I connect to DB2 from a docker container using ASP.Net Core?

  11. 11

    How to get data from 2 (or more) independent dropdown asp.net core mvc?

  12. 12

    Migrating Identity in .net core 2 class library

  13. 13

    How to decrypt .AspNetCore.Identity.Application cookie in ASP.NET Core 3.0?

  14. 14

    How do you disable HTTPS in ASP.NET Core project with Identity?

  15. 15

    How do you disable HTTPS in ASP.NET Core project with Identity?

  16. 16

    use existing login table with asp.net core identity service

  17. 17

    How to correctly download files to Angular from ASP.NET Core?

  18. 18

    How to get rolename from Roles object in asp.net core

  19. 19

    How to Export CSV file from ASP.NET core

  20. 20

    ASP.NET Core - How to get parameter from url

  21. 21

    ASP.NET Core - Cannot convert from System.security.claims.claimsPrincipal to Scheduler.Areas.Identity.Data

  22. 22

    In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role?

  23. 23

    Remove route from RouteCollection in Asp.Net Core and add new with same route name (nopCommerce-4.00)

  24. 24

    Asp.net MVC 5, Identity 2.0 Unable to add a Role to user

  25. 25

    How does Asp.Net Identity 2 User Info get mapped to IdentityServer3 profile claims

  26. 26

    ASP.NET Identity2 - How to get User Id with AllowAnonymous Controller?

  27. 27

    Angular / .net core 3.1 Get Role Claim from JWT

  28. 28

    Can't access method (Azure Tables with ASP.NET Core)

  29. 29

    How to I access the DbContext of EF core from another project when used in ASP.NET core?

ホットタグ

アーカイブ