'IdentityServer4 cannot configure certificate in code

I'm unable to use the builder methods AddDeveloperSigningCredential() or AddSigningCredential() to configure my certificate in code during app startup. If I keep the appsettings entry for Identity Server like so:

"IdentityServer": {
  "Key": {
    "Type": "Store",
    "StoreName": "My",
    "StoreLocation": "CurrentUser",
    "Name": "CN=MyApplication"
  }
}

It will take this config and find the certificate this way, ignoring the manual certificate config methods above.

If I remove the previous config from appsettings and try to use only the certificate config methods, I get an exception:

NullReferenceException: Object reference not set to an instance of an object.
Microsoft.Extensions.DependencyInjection.IdentityServerBuilderConfigurationExtensions+<>c.<AddSigningCredentials>b__10_2(IServiceProvider sp)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor<TArgument, TResult>.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)

So it seems Identity Server 4 requires this appsettings config, and I cannot override it with the certificate config methods mentioned. The methods have to be used somehow, what am I doing wrong?



Solution 1:[1]

In source codes,it may have codes like:

public static IIdentityServerBuilder AddIdentityServer(this IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<IdentityServerOptions>(configuration);
            return services.AddIdentityServer();
        }

for more details, you could check the codes https://github.com/IdentityServer/IdentityServer4/blob/main/src/AspNetIdentity/host/Startup.cs

So if you delete the related codes in appsettings.json,the options couldn't get the value

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1