IdentityServer / IdentityServer3

OpenID Connect Provider and OAuth 2.0 Authorization Server Framework for ASP.NET 4.x/Katana
https://identityserver.github.io/Documentation/
Apache License 2.0
2.01k stars 764 forks source link

Is it possible to obtain default implementations through dependency injection of extensibility points? #3411

Closed CDargis closed 7 years ago

CDargis commented 7 years ago

I'm working with 3 of the optional service factories here: https://identityserver.github.io/Documentation/docsv2/configuration/serviceFactory.html

I am in a couple scenarios where in some cases I want my own implementation of a factory to be used while in other cases I'd like to fall through to the default implementation. Right now since all of the types needed to instantiate the default implementations are registered within the DI container, I have the ability to simply subclass and have something like the following:

public class TokenService : IdentityServer3.Core.Services.Default.DefaultTokenService
{
    public TokenService(IdentityServerOptions options, IClaimsProvider claimsProvider, ITokenHandleStore tokenHandles, ITokenSigningService signingService, IEventService events)
            : base(options, claimsProvider, tokenHandles, signingService, events)
    {
    }

    //...

    public override async Task<Token> CreateAccessTokenAsync(TokenCreationRequest request)
    {
        if(...)
        {
            // I want default implementation, so fall through:
            return await base.CreateAccessTokenAsync(request);
        }
        else
        {
            // my own implementation
        }
    }
}

I'd like something different, like just taking a single object if possible (other suggestions also welcome!):

public class TokenService : ITokenService
{
    ITokenService _defaultService;
    public TokenService(DefaultTokenService defaultService)
    {
        _defaultService = defaultService;
    }

    //...

    public override async Task<Token> CreateAccessTokenAsync(TokenCreationRequest request)
    {
        if(...)
        {
            // I want default implementation, so fall through:
            return await _defaultService.CreateAccessTokenAsync(request);
        }
        else
        {
            // my own implementation
        }
    }
}

I've tried doing the above but the DI container doesn't know how to hand the DefaultTokenService to my class. Is something like this possible?

brockallen commented 7 years ago

This seems to be a general question about IdentityServer - not a bug report or an issue.

Please use StackOverflow for that. This has the advantage that questions and answers can be easily found by search engines, and that there are more people answering questions than just us.

For IdentityServer3 https://stackoverflow.com/questions/tagged/?tagnames=identityserver3&sort=newest

For IdentityServer4 https://stackoverflow.com/questions/tagged/?tagnames=identityserver4&sort=newest

For commercial support https://identityserver.io/