diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/ClaimTypeMapping.cs b/src/Microsoft.IdentityModel.JsonWebTokens/ClaimTypeMapping.cs index e2b26d0ba2..8a568086df 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/ClaimTypeMapping.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/ClaimTypeMapping.cs @@ -11,7 +11,7 @@ internal static class ClaimTypeMapping // This is the short to long mapping. // key is the long claim type // value is the short claim type - private static Dictionary shortToLongClaimTypeMapping = new Dictionary + private static readonly Dictionary shortToLongClaimTypeMapping = new Dictionary { { JwtRegisteredClaimNames.Actort, ClaimTypes.Actor }, { JwtRegisteredClaimNames.Birthdate, ClaimTypes.DateOfBirth }, @@ -88,8 +88,8 @@ internal static class ClaimTypeMapping { "winaccountname", ClaimTypes.WindowsAccountName }, }; - private static IDictionary longToShortClaimTypeMapping = new Dictionary(); - private static HashSet inboundClaimFilter = inboundClaimFilter = new HashSet(); + private static readonly Dictionary longToShortClaimTypeMapping = new Dictionary(); + private static readonly HashSet inboundClaimFilter = inboundClaimFilter = new HashSet(); /// /// Initializes static members of the class. diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs b/src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs index b95fa723f5..5f95a34a41 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs @@ -6,7 +6,6 @@ using System.Collections.Generic; using System.Globalization; using System.Security.Claims; -using System.Text; using System.Text.Json; using Microsoft.IdentityModel.Logging; using Microsoft.IdentityModel.Tokens; @@ -271,19 +270,20 @@ internal T GetValue(string key, bool throwEx, out bool found) if (typeof(T) == typeof(IList)) { - List list = new(); if (obj is IList iList) { - foreach (object item in iList) - list.Add(item?.ToString()); + string[] arr = new string[iList.Count]; + for (int arri = 0; arri < arr.Length; arri++) + { + arr[arri] = iList[arri]?.ToString(); + } - return (T)((object)list); + return (T)(object)arr; } else { - list.Add(obj.ToString()); + return (T)(object)new string[1] { obj.ToString() }; } - return (T)(object)list; } if (typeof(T) == typeof(int) && int.TryParse(obj.ToString(), out int i)) @@ -309,17 +309,20 @@ internal T GetValue(string key, bool throwEx, out bool found) if (typeof(T) == typeof(IList)) { - List list = new(); if (obj is IList items) { - foreach (object item in items) - list.Add(item); + object[] arr = new object[items.Count]; + for (int arri = 0; arri < arr.Length; arri++) + { + arr[arri] = items[arri]; + } + + return (T)(object)arr; } else { - list.Add(obj); + return (T)(object)new object[1] { obj }; } - return (T)((object)list); } if (typeof(T) == typeof(int[])) diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs index 9c6086ec73..9813cefd87 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs @@ -3,6 +3,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; using System.Security.Claims; using System.Text; using Microsoft.IdentityModel.Logging; @@ -23,7 +25,7 @@ public class JsonWebToken : SecurityToken // Some of the common values are cached in local variables. private string _act; private string _alg; - private IList _audiences; + private string[] _audiences; private string _authenticationTag; private string _ciphertext; private string _cty; @@ -614,17 +616,9 @@ public IEnumerable Audiences { lock (_audiencesLock) { - if (_audiences == null) - { - List tmp = new List(); - if (Payload.TryGetValue(JwtRegisteredClaimNames.Aud, out IList audiences)) - { - foreach (string str in audiences) - tmp.Add(str); - } - - _audiences = tmp; - } + _audiences ??= Payload.TryGetValue(JwtRegisteredClaimNames.Aud, out IList audiences) ? + (audiences is string[] audiencesArray ? audiencesArray : audiences.ToArray()) : + Array.Empty(); } } diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs index 9c3d925fbe..84ab4b325d 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebTokenHandler.cs @@ -34,7 +34,7 @@ public class JsonWebTokenHandler : TokenHandler /// /// Default claim type mapping for inbound claims. /// - public static IDictionary DefaultInboundClaimTypeMap = new Dictionary(ClaimTypeMapping.InboundClaimTypeMap); + public static readonly Dictionary DefaultInboundClaimTypeMap = new Dictionary(ClaimTypeMapping.InboundClaimTypeMap); /// /// Default value for the flag that determines whether or not the InboundClaimTypeMap is used. diff --git a/src/Microsoft.IdentityModel.Tokens/CryptoProviderFactory.cs b/src/Microsoft.IdentityModel.Tokens/CryptoProviderFactory.cs index 9c603ecd79..977968b054 100644 --- a/src/Microsoft.IdentityModel.Tokens/CryptoProviderFactory.cs +++ b/src/Microsoft.IdentityModel.Tokens/CryptoProviderFactory.cs @@ -15,8 +15,8 @@ namespace Microsoft.IdentityModel.Tokens public class CryptoProviderFactory { private static CryptoProviderFactory _default; - private static ConcurrentDictionary _typeToAlgorithmMap = new ConcurrentDictionary(); - private static object _cacheLock = new object(); + private static readonly ConcurrentDictionary _typeToAlgorithmMap = new ConcurrentDictionary(); + private static readonly object _cacheLock = new object(); private static int _defaultSignatureProviderObjectPoolCacheSize = Environment.ProcessorCount * 4; private int _signatureProviderObjectPoolCacheSize = _defaultSignatureProviderObjectPoolCacheSize; diff --git a/src/Microsoft.IdentityModel.Tokens/Encryption/SymmetricKeyWrapProvider.cs b/src/Microsoft.IdentityModel.Tokens/Encryption/SymmetricKeyWrapProvider.cs index a6dadcbe87..e142e3a338 100644 --- a/src/Microsoft.IdentityModel.Tokens/Encryption/SymmetricKeyWrapProvider.cs +++ b/src/Microsoft.IdentityModel.Tokens/Encryption/SymmetricKeyWrapProvider.cs @@ -15,8 +15,8 @@ public class SymmetricKeyWrapProvider : KeyWrapProvider private static readonly byte[] _defaultIV = new byte[] { 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6 }; private const int _blockSizeInBits = 64; private const int _blockSizeInBytes = _blockSizeInBits >> 3; - private static object _encryptorLock = new object(); - private static object _decryptorLock = new object(); + private static readonly object _encryptorLock = new object(); + private static readonly object _decryptorLock = new object(); private Lazy _symmetricAlgorithm; private ICryptoTransform _symmetricAlgorithmEncryptor; diff --git a/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs b/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs index 7f83c62d76..2494b9c6a2 100644 --- a/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs +++ b/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs @@ -112,7 +112,7 @@ namespace Microsoft.IdentityModel.Tokens /// The delegate should return a non null string that represents the 'issuer'. If null a default value will be used. /// if set, will be called before or /// - internal delegate Task IssuerValidatorAsync(string issuer, SecurityToken securityToken, TokenValidationParameters validationParameters); + internal delegate ValueTask IssuerValidatorAsync(string issuer, SecurityToken securityToken, TokenValidationParameters validationParameters); /// /// Definition for LifetimeValidator. diff --git a/src/Microsoft.IdentityModel.Tokens/Validators.cs b/src/Microsoft.IdentityModel.Tokens/Validators.cs index dccc32a547..1119918fcd 100644 --- a/src/Microsoft.IdentityModel.Tokens/Validators.cs +++ b/src/Microsoft.IdentityModel.Tokens/Validators.cs @@ -215,7 +215,10 @@ public static string ValidateIssuer(string issuer, SecurityToken securityToken, /// An EXACT match is required. internal static string ValidateIssuer(string issuer, SecurityToken securityToken, TokenValidationParameters validationParameters, BaseConfiguration configuration) { - return ValidateIssuerAsync(issuer, securityToken, validationParameters, configuration).GetAwaiter().GetResult(); + ValueTask vt = ValidateIssuerAsync(issuer, securityToken, validationParameters, configuration); + return vt.IsCompletedSuccessfully ? + vt.Result : + vt.AsTask().GetAwaiter().GetResult(); } /// @@ -232,7 +235,7 @@ internal static string ValidateIssuer(string issuer, SecurityToken securityToken /// If is null or whitespace and is null and is null. /// If 'issuer' failed to matched either or one of or . /// An EXACT match is required. - internal static async Task ValidateIssuerAsync( + internal static async ValueTask ValidateIssuerAsync( string issuer, SecurityToken securityToken, TokenValidationParameters validationParameters, diff --git a/src/Microsoft.IdentityModel.Xml/XmlUtil.cs b/src/Microsoft.IdentityModel.Xml/XmlUtil.cs index 9c0a9810f5..d00cd6abd0 100644 --- a/src/Microsoft.IdentityModel.Xml/XmlUtil.cs +++ b/src/Microsoft.IdentityModel.Xml/XmlUtil.cs @@ -14,7 +14,7 @@ namespace Microsoft.IdentityModel.Xml /// public static class XmlUtil { - private static Dictionary _hexDictionary = new Dictionary + private static readonly Dictionary _hexDictionary = new Dictionary { { 0, "0" }, { 1, "1" }, diff --git a/test/Microsoft.IdentityModel.TestUtils/ValidationDelegates.cs b/test/Microsoft.IdentityModel.TestUtils/ValidationDelegates.cs index 753f4f0fe4..f0663cc997 100644 --- a/test/Microsoft.IdentityModel.TestUtils/ValidationDelegates.cs +++ b/test/Microsoft.IdentityModel.TestUtils/ValidationDelegates.cs @@ -87,9 +87,9 @@ public static string IssuerValidatorUsingConfigEcho(string issuer, SecurityToken return issuer; } - public static Task IssuerValidatorAsync(string issuer, SecurityToken token, TokenValidationParameters validationParameters) + public static ValueTask IssuerValidatorAsync(string issuer, SecurityToken token, TokenValidationParameters validationParameters) { - return Task.FromResult(issuer); + return new ValueTask(issuer); } public static string IssuerValidatorReturnsDifferentIssuer(string issuer, SecurityToken token, TokenValidationParameters validationParameters)