Skip to content

Commit

Permalink
use string.Equals with StringComparison.OrdinalIgnoreCase instead of …
Browse files Browse the repository at this point in the history
…ToLowerInvariant() to reduce allocations in RemoteAuthenticationCallbackStrategy
  • Loading branch information
rikbosch committed Mar 20, 2023
1 parent 9bd025d commit a1658bd
Showing 1 changed file with 4 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public RemoteAuthenticationCallbackStrategy(ILogger<RemoteAuthenticationCallback

public async virtual Task<string?> 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 schemes = httpContext.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>();

foreach (var scheme in (await schemes.GetRequestHandlerSchemesAsync()).
Where(s => 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.
Expand Down Expand Up @@ -77,14 +77,14 @@ public RemoteAuthenticationCallbackStrategy(ILogger<RemoteAuthenticationCallback
var formOptions = new FormOptions { BufferBody = true, MemoryBufferThreshold = 1048576 };

var form = await httpContext.Request.ReadFormAsync(formOptions);
state = form.Where(i => 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;
}
Expand Down

0 comments on commit a1658bd

Please sign in to comment.