From 53b0980df09b75cfd3483e15fc537f6eae1d5313 Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Mon, 31 Oct 2022 20:07:10 +0000 Subject: Use TryGetValue() for dictionary lookup (#44791) Use `TryGetValue()` instead of `ContainsKey()` and the indexer. --- src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs | 4 +--- .../Authentication.Core/src/AuthenticationSchemeProvider.cs | 5 ++--- src/Identity/Core/src/SignInManager.cs | 11 +++-------- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs b/src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs index 6dc39a5376..6dce130d73 100644 --- a/src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs +++ b/src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs @@ -147,7 +147,7 @@ public class DictionaryAdapter : IAdapter } // As per JsonPatch spec, the target location must exist for test to be successful - if (!dictionary.ContainsKey(convertedKey)) + if (!dictionary.TryGetValue(convertedKey, out var currentValue)) { errorMessage = Resources.FormatTargetLocationAtPathSegmentNotFound(segment); return false; @@ -158,8 +158,6 @@ public class DictionaryAdapter : IAdapter return false; } - var currentValue = dictionary[convertedKey]; - // The target segment does not have an assigned value to compare the test value with if (currentValue == null) { diff --git a/src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs b/src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs index bab572a374..1881ff157c 100644 --- a/src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs +++ b/src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs @@ -184,15 +184,14 @@ public class AuthenticationSchemeProvider : IAuthenticationSchemeProvider /// The name of the authenticationScheme being removed. public virtual void RemoveScheme(string name) { - if (!_schemes.ContainsKey(name)) + if (!_schemes.TryGetValue(name, out var scheme)) { return; } lock (_lock) { - if (_schemes.ContainsKey(name)) + if (_schemes.TryGetValue(name, out scheme)) { - var scheme = _schemes[name]; if (_requestHandlers.Remove(scheme)) { _requestHandlersCopy = _requestHandlers.ToArray(); diff --git a/src/Identity/Core/src/SignInManager.cs b/src/Identity/Core/src/SignInManager.cs index e6af0da9de..9b93a56d23 100644 --- a/src/Identity/Core/src/SignInManager.cs +++ b/src/Identity/Core/src/SignInManager.cs @@ -652,26 +652,21 @@ public class SignInManager where TUser : class { var auth = await Context.AuthenticateAsync(IdentityConstants.ExternalScheme); var items = auth?.Properties?.Items; - if (auth?.Principal == null || items == null || !items.ContainsKey(LoginProviderKey)) + if (auth?.Principal == null || items == null || !items.TryGetValue(LoginProviderKey, out var provider)) { return null; } if (expectedXsrf != null) { - if (!items.ContainsKey(XsrfKey)) - { - return null; - } - var userId = items[XsrfKey] as string; - if (userId != expectedXsrf) + if (!items.TryGetValue(XsrfKey, out var userId) || + userId != expectedXsrf) { return null; } } var providerKey = auth.Principal.FindFirstValue(ClaimTypes.NameIdentifier); - var provider = items[LoginProviderKey] as string; if (providerKey == null || provider == null) { return null; -- cgit v1.2.3