diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs index ee52657cc0..cda7141ffd 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs @@ -56,13 +56,11 @@ public JsonWebToken(string jwtEncodedString) if (string.IsNullOrEmpty(jwtEncodedString)) throw new ArgumentNullException(nameof(jwtEncodedString)); - // Number of segments is always one more than the number of '.'s. - var count = jwtEncodedString.Count(c => c == '.') + 1; - if (count == JwtConstants.JwsSegmentCount || count == JwtConstants.JweSegmentCount) - { - var tokenParts = jwtEncodedString.Split('.'); + // Max number of segments is set to JwtConstants.MaxJwtSegmentCount + 1 so that we know if there were more than 5 segments present. + // In the case where JwtEncodedString has greater than 5 segments, the length of tokenParts will always be 6. + var tokenParts = jwtEncodedString.Split(new char[] { '.' }, JwtConstants.MaxJwtSegmentCount + 1); + if (tokenParts.Length == JwtConstants.JwsSegmentCount || tokenParts.Length == JwtConstants.JweSegmentCount) Decode(tokenParts, jwtEncodedString); - } else throw LogHelper.LogExceptionMessage(new ArgumentException(LogHelper.FormatInvariant(LogMessages.IDX14100, jwtEncodedString))); }