From 9bd025dafd3492dde5181ec0f6df109a613139e2 Mon Sep 17 00:00:00 2001 From: Rik Bosch <549975+rikbosch@users.noreply.github.com> Date: Mon, 20 Mar 2023 15:20:22 +0100 Subject: [PATCH 1/2] Reduce allocations and improve performance of BasePathStrategy --- .../Strategies/BasePathStrategy.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Finbuckle.MultiTenant.AspNetCore/Strategies/BasePathStrategy.cs b/src/Finbuckle.MultiTenant.AspNetCore/Strategies/BasePathStrategy.cs index 4846a4b6..fe3dddd5 100644 --- a/src/Finbuckle.MultiTenant.AspNetCore/Strategies/BasePathStrategy.cs +++ b/src/Finbuckle.MultiTenant.AspNetCore/Strategies/BasePathStrategy.cs @@ -11,14 +11,14 @@ public class BasePathStrategy : IMultiTenantStrategy { public Task GetIdentifierAsync(object context) { - if(!(context is HttpContext httpContext)) + if (!(context is HttpContext httpContext)) throw new MultiTenantException(null, new ArgumentException($"\"{nameof(context)}\" type must be of type HttpContext", nameof(context))); var path = httpContext.Request.Path; var pathSegments = - path.Value?.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); + path.Value?.Split('/', 2, StringSplitOptions.RemoveEmptyEntries); if (pathSegments is null || pathSegments.Length == 0) return Task.FromResult(null); From a1658bdfa4c7762f2727b62844ff2164b0d68986 Mon Sep 17 00:00:00 2001 From: Rik Bosch <549975+rikbosch@users.noreply.github.com> Date: Mon, 20 Mar 2023 15:21:19 +0100 Subject: [PATCH 2/2] use string.Equals with StringComparison.OrdinalIgnoreCase instead of ToLowerInvariant() to reduce allocations in RemoteAuthenticationCallbackStrategy --- .../Strategies/RemoteAuthenticationCallbackStrategy.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Finbuckle.MultiTenant.AspNetCore/Strategies/RemoteAuthenticationCallbackStrategy.cs b/src/Finbuckle.MultiTenant.AspNetCore/Strategies/RemoteAuthenticationCallbackStrategy.cs index 1ef8ef07..dcb04a18 100644 --- a/src/Finbuckle.MultiTenant.AspNetCore/Strategies/RemoteAuthenticationCallbackStrategy.cs +++ b/src/Finbuckle.MultiTenant.AspNetCore/Strategies/RemoteAuthenticationCallbackStrategy.cs @@ -27,7 +27,7 @@ public RemoteAuthenticationCallbackStrategy(ILogger GetIdentifierAsync(object context) { - if(!(context is HttpContext httpContext)) + if (!(context is HttpContext httpContext)) throw new MultiTenantException(null, new ArgumentException($"\"{nameof(context)}\" type must be of type HttpContext", nameof(context))); @@ -35,7 +35,7 @@ public RemoteAuthenticationCallbackStrategy(ILogger typeof(IAuthenticationRequestHandler).IsAssignableFrom(s.HandlerType))) - // Where(s => s.HandlerType.ImplementsOrInheritsUnboundGeneric(typeof(RemoteAuthenticationHandler<>)))) + // Where(s => s.HandlerType.ImplementsOrInheritsUnboundGeneric(typeof(RemoteAuthenticationHandler<>)))) { // Unfortnately we can't rely on the ShouldHandleAsync method since OpenId Connect handler doesn't use it. // Instead we'll get the paths to check from the options. @@ -77,14 +77,14 @@ public RemoteAuthenticationCallbackStrategy(ILogger i.Key.ToLowerInvariant() == "state").Single().Value; + state = form.Single(i => string.Equals(i.Key, "state", StringComparison.OrdinalIgnoreCase)).Value; } var properties = ((dynamic)options).StateDataFormat.Unprotect(state) as AuthenticationProperties; if (properties == null) { - if(logger != null) + if (logger != null) logger.LogWarning("A tenant could not be determined because no state paraameter passed with the remote authentication callback."); return null; }