AliBazzi / IdentityServer4.Contrib.RedisStore

A persistence layer using Redis DB for operational data and for caching capability for Identity Server 4
https://www.nuget.org/packages/IdentityServer4.Contrib.RedisStore
MIT License
137 stars 48 forks source link

Unable to resolve service for type 'IdentityServer4.EntityFramework.Interfaces.IConfigurationDbContext' #27

Closed audriuiv closed 4 years ago

audriuiv commented 4 years ago

IdentityServer4 3.0 Startup file

services.AddDbContext<ApplicationDbContext>(options =>
                options.UseMySql(Configuration.GetConnectionString("MySQLConnection"),
                    mySqlOptions =>
                    {
                        mySqlOptions.ServerVersion(new Version("8.0"), ServerType.MySql);
                    }
            ));
....
 services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

 var builder = services.AddIdentityServer(options =>
                {
                    options.Events.RaiseErrorEvents = true;
                    options.Events.RaiseInformationEvents = true;
                    options.Events.RaiseFailureEvents = true;
                    options.Events.RaiseSuccessEvents = true;
                })
                .AddOperationalStore(options =>
                {
                    options.RedisConnectionString = "redis-14781.c13.us-east-xx-cloud.redislabs.com:10000,password=mypassword";
                    options.Db = 0;
                })
                .AddRedisCaching(options =>
                {                   
                    options.KeyPrefix = "xy";
                })
            // using IdentityServer4.EntityFramework.Stores
                .AddClientStoreCache<ClientStore>()
                .AddResourceStoreCache<ResourceStore>()
                .AddCorsPolicyCache<CorsPolicyService>()
                .AddAspNetIdentity<ApplicationUser>();

              /*
                In memory have worked perfected.  
               .AddInMemoryIdentityResources(Config.Ids)
              .AddInMemoryApiResources(Config.Apis)
              .AddInMemoryClients(Config.Clients)
              .AddAspNetIdentity<ApplicationUser>();
              */

 var AppSettings = Configuration.GetSection("CertificateData");
            builder.AddSigningCredential(
                X509.GetCertificate(
                    AppSettings["Thumbprint"],
                    AppSettings["File"],
                    AppSettings["Path"],
                    AppSettings["Password"]
            ));

            services.AddAuthentication();

Error:

Exception thrown: 'System.InvalidOperationException' in System.Private.CoreLib.dll
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
Unhandled exception. System.InvalidOperationException: Unable to resolve service for type 'IdentityServer4.EntityFramework.Interfaces.IConfigurationDbContext' while attempting to activate 'IdentityServer4.EntityFramework.Stores.ClientStore'.

Without Redis App works perfect.

AliBazzi commented 4 years ago

Looking at this:

/*
                In memory have worked perfected.  
               .AddInMemoryIdentityResources(Config.Ids)
              .AddInMemoryApiResources(Config.Apis)
              .AddInMemoryClients(Config.Clients)
              .AddAspNetIdentity<ApplicationUser>();
              */

you can't remove the in memory stores without using your own or other stores like IdentityServer4.EntityFramework

the exception you are facing is not related to the library, maybe you need to take sometime to understand how identity server works.

audriuiv commented 4 years ago

Thanks, So I want to use IdentityServer4.Contrib.RedisStore for (AddOperationalStore, AddRedisCaching) but if I use AddInMemoryClients then clients cache will be saved in memory (not in Redis)? And I have to write own my stories?

AliBazzi commented 4 years ago

If you want to use Redis caching then it means you you are using slower store for configuration data, using in memory stores are not recommended for production payloads, but you know better around you case.

For the operational data, the library can help and be an alternative for storage that is already available for relational stores, like SQL/MySQL .. etc

The one that I already pointed at earlier supports storing/reading configuration and operational data using relational store, while this library can help with caching and having faster operational stores.

Hope this would be useful for you.

audriuiv commented 4 years ago

Thanks a lot.