From 178e2a14ccf7e6bef54c37aeb39c53104c13332e Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 27 Nov 2024 23:54:08 +1100 Subject: [PATCH] remove duplicate dictionary lookups --- .../DependencyInjection/Options/DynamicProviderOptions.cs | 2 +- .../Endpoints/Results/ProtectedResourceErrorResult.cs | 4 ++-- .../LocalApiAuthentication/LocalApiAuthenticationHandler.cs | 3 +-- src/IdentityServer/Test/TestUserStore.cs | 4 ++-- .../TestFramework/TestBrowserClient.cs | 3 +-- 5 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/IdentityServer/Configuration/DependencyInjection/Options/DynamicProviderOptions.cs b/src/IdentityServer/Configuration/DependencyInjection/Options/DynamicProviderOptions.cs index abe7e0720..a21b0a0db 100644 --- a/src/IdentityServer/Configuration/DependencyInjection/Options/DynamicProviderOptions.cs +++ b/src/IdentityServer/Configuration/DependencyInjection/Options/DynamicProviderOptions.cs @@ -76,7 +76,7 @@ public void AddProviderType(string type) /// public DynamicProviderType? FindProviderType(string type) { - return _providers.ContainsKey(type) ? _providers[type] : null; + return _providers.GetValueOrDefault(type); } /// diff --git a/src/IdentityServer/Endpoints/Results/ProtectedResourceErrorResult.cs b/src/IdentityServer/Endpoints/Results/ProtectedResourceErrorResult.cs index 6db2339c6..b897fd7f2 100644 --- a/src/IdentityServer/Endpoints/Results/ProtectedResourceErrorResult.cs +++ b/src/IdentityServer/Endpoints/Results/ProtectedResourceErrorResult.cs @@ -48,9 +48,9 @@ public Task WriteHttpResponse(ProtectedResourceErrorResult result, HttpContext c var error = result.Error; var errorDescription = result.ErrorDescription; - if (Constants.ProtectedResourceErrorStatusCodes.ContainsKey(error)) + if (Constants.ProtectedResourceErrorStatusCodes.TryGetValue(error, out var code)) { - context.Response.StatusCode = Constants.ProtectedResourceErrorStatusCodes[error]; + context.Response.StatusCode = code; } if (error == OidcConstants.ProtectedResourceErrors.ExpiredToken) diff --git a/src/IdentityServer/Hosting/LocalApiAuthentication/LocalApiAuthenticationHandler.cs b/src/IdentityServer/Hosting/LocalApiAuthentication/LocalApiAuthenticationHandler.cs index 4857f49ee..ff137cd9d 100644 --- a/src/IdentityServer/Hosting/LocalApiAuthentication/LocalApiAuthenticationHandler.cs +++ b/src/IdentityServer/Hosting/LocalApiAuthentication/LocalApiAuthenticationHandler.cs @@ -246,10 +246,9 @@ protected override Task HandleChallengeAsync(AuthenticationProperties properties var nonce = Context.Items["DPoP-Nonce"] as string; Context.Response.Headers[HttpHeaders.DPoPNonce] = nonce; } - else if (properties.Items.ContainsKey("DPoP-Nonce")) + else if (properties.Items.TryGetValue("DPoP-Nonce", out var nonce)) { // this allows the API itself to set the nonce - var nonce = properties.Items["DPoP-Nonce"]; Context.Response.Headers[HttpHeaders.DPoPNonce] = nonce; } } diff --git a/src/IdentityServer/Test/TestUserStore.cs b/src/IdentityServer/Test/TestUserStore.cs index bad3d63b3..d77394e74 100644 --- a/src/IdentityServer/Test/TestUserStore.cs +++ b/src/IdentityServer/Test/TestUserStore.cs @@ -103,9 +103,9 @@ public TestUser AutoProvisionUser(string provider, string userId, List cl filtered.Add(new Claim(JwtClaimTypes.Name, claim.Value)); } // if the JWT handler has an outbound mapping to an OIDC claim use that - else if (JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.ContainsKey(claim.Type)) + else if (JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.TryGetValue(claim.Type, out var value)) { - filtered.Add(new Claim(JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap[claim.Type], claim.Value)); + filtered.Add(new Claim(value, claim.Value)); } // copy the claim as-is else diff --git a/test/IdentityServer.IntegrationTests/TestFramework/TestBrowserClient.cs b/test/IdentityServer.IntegrationTests/TestFramework/TestBrowserClient.cs index 4fc1a8954..8bf91045e 100644 --- a/test/IdentityServer.IntegrationTests/TestFramework/TestBrowserClient.cs +++ b/test/IdentityServer.IntegrationTests/TestFramework/TestBrowserClient.cs @@ -268,8 +268,7 @@ public string this[string key] { get { - if (Inputs.ContainsKey(key)) return Inputs[key]; - return null; + return Inputs.GetValueOrDefault(key); } set {