Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Costello <martin@martincostello.com>2022-10-31 23:07:10 +0300
committerGitHub <noreply@github.com>2022-10-31 23:07:10 +0300
commit53b0980df09b75cfd3483e15fc537f6eae1d5313 (patch)
treea9e486e5a9ff14f724d374b623a2fe28b8bfc7a8
parentf200c9564ab98670764eb78fadf722d060898f36 (diff)
Use TryGetValue() for dictionary lookup (#44791)
Use `TryGetValue()` instead of `ContainsKey()` and the indexer.
-rw-r--r--src/Features/JsonPatch/src/Internal/DictionaryAdapterOfTU.cs4
-rw-r--r--src/Http/Authentication.Core/src/AuthenticationSchemeProvider.cs5
-rw-r--r--src/Identity/Core/src/SignInManager.cs11
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<TKey, TValue> : 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<TKey, TValue> : 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
/// <param name="name">The name of the authenticationScheme being removed.</param>
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<TUser> 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;