0

So, IResourceOwnerPasswordValidator is supposed to replace IUserService. In the old version IdentityServer3 I could do the following:

factory.UserService = new Registration<IUserService>(typeof(MyIdentityUserService));
factory.Register(new Registration<IUserRepository>(userRepository));

which passes a custom userRepository object with the proper connection string. Now in IdentityServer4 I need to Connect to the proper Portal database in order for ID4 to authenticate the user against the correct portal database.

If I could pass a parameter to the constructor of the ResourceOwnerPasswordValidator class like below , then this would be fine but this is not possible with ASP.NET CORE.

 public ResourceOwnerPasswordValidator(string MyConnString)
        {
            _connstring = MyConnString;
        }

This can't work because the ConfigurationService registers this class without the ability to pass values. This makes sense since this registration process begins before the pipeline is built. How do I dynamically pass a connection string to the ResourceOwnerPasswordValidator class? I am trying to authenticate against the portal making the call. In ID3 I could do this by using the acr_value.

I am trying to get ID4 to authenticate as a MultiTenant Authentication Server.

Nate
  • 2,044
  • 4
  • 23
  • 47

1 Answers1

0

You could inject a service or a DTO into a ResourceOwnerPasswordValidator constructor:

public class ResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
{
    public ResourceOwnerPasswordValidator(IAuthRepository repository)
    {
    }
}

Hence it is possible to pass a service which returns your connection string.

By default, validator's implementation is registered as Transient which means it will not be a singleton until it is resolved in a singleton (which is not a IdSrv4 case I guess).

If you need a Scoped connection string (i.e. per web request), register a scoped DTO with a factory method: services.AddScoped<T>(() => ) or use AsyncLocal<T>. Check this thread to get more details:

How to Per-Request caching in ASP.net core

Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114