Skip to content

Commit 169b5d2

Browse files
committed
Fixed issue with resolving of IApiKeyProvider implementation when using multiple schemas.
1 parent 032bbd2 commit 169b5d2

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/AspNetCore.Authentication.ApiKey/ApiKeyHandlerBase.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -178,21 +178,28 @@ private async Task<AuthenticateResult> RaiseAndHandleAuthenticationSucceededAsyn
178178

179179
private async Task<IApiKey> ValidateUsingApiKeyProviderAsync(string apiKey)
180180
{
181-
var apiKeyProvider = Context.RequestServices.GetService<IApiKeyProvider>();
181+
IApiKeyProvider apiKeyProvider = null;
182+
if (Options.ApiKeyProviderType != null)
183+
{
184+
apiKeyProvider = ActivatorUtilities.GetServiceOrCreateInstance(Context.RequestServices, Options.ApiKeyProviderType) as IApiKeyProvider;
185+
}
186+
182187
if (apiKeyProvider == null)
183188
{
184-
if (Options.ApiKeyProviderType != null)
185-
{
186-
apiKeyProvider = Context.RequestServices.GetService(Options.ApiKeyProviderType) as IApiKeyProvider;
187-
}
188-
189-
if (apiKeyProvider == null)
190-
{
191-
throw new InvalidOperationException($"Either {nameof(Options.Events.OnValidateKey)} delegate on configure options {nameof(Options.Events)} should be set or an implementaion of {nameof(IApiKeyProvider)} should be registered in the dependency container.");
192-
}
189+
throw new InvalidOperationException($"Either {nameof(Options.Events.OnValidateKey)} delegate on configure options {nameof(Options.Events)} should be set or use an extention method with type parameter of type {nameof(IApiKeyProvider)}.");
193190
}
194191

195-
return await apiKeyProvider.ProvideAsync(apiKey).ConfigureAwait(false);
192+
try
193+
{
194+
return await apiKeyProvider.ProvideAsync(apiKey).ConfigureAwait(false);
195+
}
196+
finally
197+
{
198+
if (apiKeyProvider is IDisposable disposableApiKeyProvider)
199+
{
200+
disposableApiKeyProvider.Dispose();
201+
}
202+
}
196203
}
197204
}
198205
}

src/AspNetCore.Authentication.ApiKey/ApiKeyPostConfigureOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public void PostConfigure(string name, ApiKeyOptions options)
2525

2626
if (options.Events?.OnValidateKey == null && options.EventsType == null && options.ApiKeyProviderType == null)
2727
{
28-
throw new InvalidOperationException($"Either {nameof(options.Events.OnValidateKey)} delegate on {nameof(options.Events)} property of configure options {typeof(ApiKeyOptions).Name} should be set or an implementaion of {typeof(IApiKeyProvider).Name} should be registered in the dependency container.");
28+
throw new InvalidOperationException($"Either {nameof(ApiKeyOptions.Events.OnValidateKey)} delegate on configure options {nameof(ApiKeyOptions.Events)} should be set or use an extention method with type parameter of type {nameof(IApiKeyProvider)}.");
2929
}
3030
}
3131
}

0 commit comments

Comments
 (0)