Exemplo n.º 1
0
    private async Task <AuthenticationScheme> GetDynamicSchemeAsync(string name)
    {
        if (_httpContextAccessor.HttpContext == null)
        {
            _logger.LogDebug("IAuthenticationSchemeProvider being used outside HTTP request, therefore dynamic provider feature can't be used for loading scheme: {scheme}.", name);
            return(null);
        }

        // these have to be here because the regular authentication middleware accepts IAuthenticationSchemeProvider
        // as a ctor param, not an Invoke param, which makes it a singleton. Our DynamicAuthenticationSchemeCache
        // and possibly the store is scoped in DI.
        var cache = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService <DynamicAuthenticationSchemeCache>();
        var store = _httpContextAccessor.HttpContext.RequestServices.GetRequiredService <IIdentityProviderStore>();

        var dynamicScheme = cache.Get(name);

        if (dynamicScheme == null)
        {
            var idp = await store.GetBySchemeAsync(name);

            if (idp != null && idp.Enabled)
            {
                var providerType = _options.FindProviderType(idp.Type);
                if (providerType != null)
                {
                    LicenseValidator.ValidateDynamicProviders();
                    dynamicScheme = new DynamicAuthenticationScheme(idp, providerType.HandlerType);
                    cache.Add(name, dynamicScheme);
                }
            }
        }

        return(dynamicScheme);
    }