0

For the past 2 days I have been trying to get ApiAuthorizationDbContext to set a custom IdentityRole. With IdentityDbContext this could be done as follows:

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

In the blazor project template the declaration of the ApplicationDbContext is the following:

public class ApplicationDbContext : ApiAuthorizationDbContext<ApplicationUser>
{
    public ApplicationDbContext(
        DbContextOptions options,
        IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
    {
    }
}

How can I do something similar with the ApiAuthorizationDbContext. I can not find this kind of support from the ApiAuthorizationDbContext class.

Thanks.

pitaridis
  • 2,801
  • 3
  • 22
  • 41
  • Can this be to any help? https://medium.com/@marcodesanctis2/role-based-security-with-blazor-and-identity-server-4-aba12da70049 – Roar S. Nov 29 '20 at 21:59
  • I want to create a custom Roles class which inherits from IdentityRole and inform the ApiAuthorizationDbContext that I have a custom class to use instead of the default IdentityRole. – pitaridis Nov 30 '20 at 11:59
  • 1
    Have you tried checking this one? https://stackoverflow.com/questions/58208894/asp-net-core-identity-custom-apiauthorizationdbcontext – MADJOE Apr 22 '21 at 04:11

2 Answers2

1

From @javiercn

ApiAuthorizationDbContext is a convenience class to get you started.

https://github.com/dotnet/aspnetcore/issues/14161#issuecomment-533468760

I created this ApplicationApiAuthorizationDbContext to get it working with roles.

//Based on Microsoft.AspNetCore.ApiAuthorization.IdentityServer.ApiAuthorizationDbContext, Version=6.0.2.0
//https://github.com/dotnet/aspnetcore/issues/14161#issuecomment-533468760
public class ApplicationApiAuthorizationDbContext<TUser, TRole> : IdentityDbContext<TUser, TRole, string>, IPersistedGrantDbContext, IDisposable where TUser : IdentityUser where TRole : IdentityRole
{
    private readonly IOptions<OperationalStoreOptions> _operationalStoreOptions;

    public DbSet<PersistedGrant> PersistedGrants
    {
        get;
        set;
    }

    public DbSet<DeviceFlowCodes> DeviceFlowCodes
    {
        get;
        set;
    }

    public DbSet<Key> Keys
    {
        get;
        set;
    }

    public ApplicationApiAuthorizationDbContext(DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions)
        : base(options)
    {
        _operationalStoreOptions = operationalStoreOptions;
    }

    Task<int> IPersistedGrantDbContext.SaveChangesAsync()
    {
        return base.SaveChangesAsync();
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        builder.ConfigurePersistedGrantContext(_operationalStoreOptions.Value);
    }
}

Complete examples with added roles in EF Core 5.0 and EF Core 6.0:

https://stackoverflow.com/a/71321924/3850405

Ogglas
  • 62,132
  • 37
  • 328
  • 418
0

I did not find a way to have my custom role class but I found a way to solve my problem and add properties to the existing IdentityRole class and will answer my question just in case someone else needs to do something similar. The solution to this problem is the Shadow properties.

For example if I want to create a DateTime property to the IdentityRole class we can use the following code in our DBContext class.

protected override void OnModelCreating(ModelBuilder builder)
{
    builder.Entity<IdentityRole>().Property<DateTime>("LastUpdated");
    base.OnModelCreating(builder);
}

Now we can access the property like this:

context.Entry(myRole).Property("LastUpdated").CurrentValue = DateTime.Now;

It is not as easy as having a custom class but at least it gives me the opportunity to have custom properties for the IdentityRole class.

pitaridis
  • 2,801
  • 3
  • 22
  • 41