The type UserDBContext cannot be used as a type parameter ‘TContext’ in the generic type of method ‘AddApiAuthorization’

I’m trying to set up Authorization using Identity for my React ASP.NET Core app. I’m following the ASP.NET Core docs: Docs

In the previous examples I’m using my ‘UserDBContext’ wherever ‘ApplicationContext’ is referenced in the docs without trouble but in this particular instance I’m getting an error.

I’m running into an error when I try to configure the ‘AddIdentityServer’ service. In my Startup.cs

Full Error:

The type 'projectName.UserDBContext' cannot be used as type parameter 'TContext' in the generic type or method 'IdentityServerBuilderConfigurationExtensions.AddApiAuthorization<TUser, TContext>(IIdentityServerBuilder)'. There is no implicit reference conversion from 'projectName.UserDBContext' to 'IdentityServer4.EntityFramework.Interfaces.IPersistedGrantDbContext'

User.cs:

namespace project {

    public class User : IdentityUser {

        [DataType (DataType.Date)]
        public DateTime created { get; set; }

        [Key]
        [Required]
        public string email { get; set; }

        [Required]
        public string firstname { get; set; }

        [Required]
        public string lastname { get; set; }

        [Required]
        [Range (1, 100)]
        public string username { get; set; }

        [Required]
        [StringLength (100)]
        public string password { get; set; }

    }

    public class UserDBContext : IdentityDbContext {

        public DbSet<User> users { get; set; }

        public UserDBContext (DbContextOptions<UserDBContext> options) : base (options) { }

    }
}

Startup.cs

        public void ConfigureServices (IServiceCollection services) {

            services.AddControllersWithViews ();

            services.AddSpaStaticFiles (configuration => {
                configuration.RootPath = "ClientApp/build";
            });

            services.AddDbContext<UserDBContext> (options =>
                options.UseNpgsql (Configuration.GetConnectionString ("DefaultConnection")));

            services.AddDefaultIdentity<User> ()
                .AddEntityFrameworkStores<UserDBContext> ();

            services.AddIdentityServer()
                .AddApiAuthorization<User, UserDBContext>();


            services.AddAuthentication ()
                .AddIdentityServerJwt ();

        }

This is the code that is giving me the error:

            services.AddIdentityServer()
                .AddApiAuthorization<User, UserDBContext>();

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Try something like that:

public class UserDBContext : IdentityDbContext,IPersistedGrantDbContext {

    public DbSet<User> users { get; set; }

    public UserDBContext (DbContextOptions<UserDBContext> options) : base (options) { }

}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x