Search code examples
c#asp.net-coreasp.net-identityrolesrbac

Asp.net core role based access identity / roles make user table self referencing


Hi I am trying to model an access control system where access is granted by role to certain actions. im trying to use asp.net core with identity and roles. it seems the default identity comes with its own tables for users. The users table i have need to be self referencing as a users will have managers who are also users.

is it possible to make the default table that comes with identity do that?


Solution

  • If you want to add additional properties to the already existing IdentityUser, all you have to do is inherit the IdentityUser class and then add your own properties then you go to the startup file and your dbcontext and do the following

    public class InheritedUser : IdentityUser
    {
       public bool HasTwoHeads { get; set;}
    
    }
    

    your startup

    services.AddDefaultIdentity<InheritedUser>()
                    .AddEntityFrameworkStores<CustomDbContext>()                               .AddDefaultTokenProviders()
    .AddDefaultUI(Microsoft.AspNetCore.Identity.UI.UIFramework.Bootstrap4);
    

    Then your Inherited DbContext

    public class InheritedDbContext : IdentityDbContex<InheritedUser>
    {
    
    }