Aspnetcore: ApiAuthorizationDbContext<TUser> force TUser to extends IdentityUser instead of IdentityUser<TKey>

Created on 19 Apr 2019  路  10Comments  路  Source: dotnet/aspnetcore

Describe the bug

At the moment, ApiAuthorizationDbContext<TUser> force TUser to extends IdentityUser instead of IdentityUser<TKey>. This means it is impossible to use Identity Server on application with ApplicationUser : IdentityUser<Guid> (or anything different from IdentityUser<string>)

To Reproduce

Steps to reproduce the behavior:

  1. Using ASP.NET Core 3.0 Preview 4
  2. Create a new React or Angular template with Identity
  3. Change ApplicationUser to extends IdentityUser<Guid> instead of IdentityUser
  4. Get error: Error CS0311 The type 'WebApplication.Models.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'ApiAuthorizationDbContext<TUser>'. There is no implicit reference conversion from 'WebApplication.Models.ApplicationUser' to 'Microsoft.AspNetCore.Identity.IdentityUser'.

Expected behavior

I would expect ApiAuthorizationDbContext to accept another generic to indicate the key type (e.g. ApiAuthorizationDbContext<ApplicationUser, Guid>)

Won't Fix Resolved area-identity

Most helpful comment

2020 Looks like it was not fixed yet...

Am I wrong?

All 10 comments

@HaoK @blowdart Where does ApiAuthorizationDbContext come from?

Part of the identity server support I believe. @javiercn ?

ApiAuthorizationDbContext is just a convenience class for the templates.

We don鈥檛 plan to support all the variants IdentityDbContext supports.

If you want to customize the key you can extend IdentityDbContext directly and implement IPersistentGrantDbContext as ApiAuthorizationDbContext does.

https://github.com/aspnet/AspNetCore/blob/master/src/Identity/ApiAuthorization.IdentityServer/src/Data/ApiAuthorizationDbContext.cs

Re-opening, since ApiAuthorizationDbContext doesn't follow best practices for a DbContext shipped for others to use. In particular:

  • It should expose a public constructor that takes a generic DbContextOptions<TContext>. Otherwise people resolving this directly will have issues when multiple contexts are registered in D.I.
  • It should expose a protected constructor that takes a non-generic DbContextOptions for use when other classes inherit from it.

@javiercn Is this the correct repo to track this issue? If not, where should I move it?

@ajcvickers This is the correct repo.

Could we use a separate issue to track the items you mention?

2020 Looks like it was not fixed yet...

Am I wrong?

@blowdart , @ajcvickers - Would you accept a PR from the community on this one? I have opened up #21100 that should hopefully address everything. Thanks for your consideration.

We've decided not to support this

The idea is that apps can derive from IdentityDbContext directly and utilize the generic support there, this class is just meant to be sugar for the most common scenario

Was this page helpful?
0 / 5 - 0 ratings