From 88fd735e67556e34719ed431d33725e1ab33134c Mon Sep 17 00:00:00 2001 From: Lasse Gaardsholt Date: Fri, 10 Jul 2020 19:51:26 +0200 Subject: [PATCH] Changed the ApiKeyMiddleware to accept an array of apiKeys instead of a string --- ApiKeyAuthMiddleware.cs | 6 +++--- Startup.cs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ApiKeyAuthMiddleware.cs b/ApiKeyAuthMiddleware.cs index 54fdb7e..b1e80df 100644 --- a/ApiKeyAuthMiddleware.cs +++ b/ApiKeyAuthMiddleware.cs @@ -39,7 +39,7 @@ public class ApiKeyAuthOptions : AuthenticationSchemeOptions /// Your super secret value that should be in "KeyName" /// If you need to specify multiple keys then do it as comma seperated. /// - public string ApiKeys { get; set; } + public string[] ApiKeys { get; set; } } internal class ApiKeyAuthHandler : AuthenticationHandler @@ -54,7 +54,7 @@ protected override Task HandleAuthenticateAsync() var identity = new ClaimsIdentity(Options.KeyName); var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), null, Options.KeyName); - if (string.IsNullOrWhiteSpace(Options.ApiKeys)) + if (Options.ApiKeys.Length == 0) return Task.FromResult(AuthenticateResult.Success(ticket)); @@ -67,7 +67,7 @@ protected override Task HandleAuthenticateAsync() - if (Options.ApiKeys.Split(',').Contains(key)) + if (Options.ApiKeys.Contains(key)) return Task.FromResult(AuthenticateResult.Success(ticket)); diff --git a/Startup.cs b/Startup.cs index 90e74bb..4703530 100644 --- a/Startup.cs +++ b/Startup.cs @@ -26,8 +26,8 @@ public void ConfigureServices(IServiceCollection services) services.AddSingleton(new Settings()); services.AddApiKeyAuthentication(a => { - a.ApiKeys = Settings.API_KEYS; a.KeyName = "ApiKey"; + a.ApiKeys = Settings.API_KEYS.Split(','); });