Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,9 @@ internal static string GetStringClaimValueType(string str, string claimType)
if (!string.IsNullOrEmpty(claimType) && !AppContextSwitches.TryAllStringClaimsAsDateTime && JsonSerializerPrimitives.IsKnownToNotBeDateTime(claimType))
return ClaimValueTypes.String;

if (DateTime.TryParse(str, out DateTime dateTimeValue))
if (DateTime.TryParse(str, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out _))
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to use DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal is broader than just handling trailing zeros. This will now classify ANY parseable DateTime string as a DateTime claim, including:

  • Date-only strings like "2019-11-15" (previously String, now DateTime)
  • Ambiguous formats like "01/02/2023"
  • Relative date strings that DateTime.TryParse might accept

This is a significant behavioral change beyond the trailing zeros issue described in the PR. Consider:

  1. Adding comprehensive tests for various date formats to document the new behavior
  2. Verifying this doesn't break existing applications that expect certain date strings to remain as String type
  3. Evaluating if DateTimeStyles.RoundtripKind (used in JsonSerializerPrimitives.cs line 151) would be more appropriate to maintain consistency and only accept ISO8601 roundtrip formats
Suggested change
if (DateTime.TryParse(str, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out _))
if (DateTime.TryParseExact(str, "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out _))

Copilot uses AI. Check for mistakes.
{
string dtUniversal = dateTimeValue.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture);
if (dtUniversal.Equals(str, StringComparison.Ordinal))
return ClaimValueTypes.DateTime;
return ClaimValueTypes.DateTime;
}

return ClaimValueTypes.String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
Expand Down Expand Up @@ -218,6 +219,21 @@ public void ResolveTokenSigningKey()
Assert.Null(resolvedKey);
}

#region GetStringClaimValueType

[Fact]
public void GetStringClaimValueType_ReturnsDateTime_ForIso8601WithOrWithoutTrailingZeros()
{
var iso8601WithTrailingZeros = "2025-11-26T09:30:45.1234560Z";
var iso8601WithoutTrailingZeros = "2025-11-26T09:30:45.123456Z";
var resultWithTrailingZeros = JwtTokenUtilities.GetStringClaimValueType(iso8601WithTrailingZeros);
var resultWithoutTrailingZeros = JwtTokenUtilities.GetStringClaimValueType(iso8601WithoutTrailingZeros);
Assert.Equal(ClaimValueTypes.DateTime, resultWithTrailingZeros);
Assert.Equal(ClaimValueTypes.DateTime, resultWithoutTrailingZeros);
}
Comment on lines +224 to +233
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test cases for edge cases beyond trailing zeros, such as:

  • Date-only strings: "2019-11-15"
  • Various ISO8601 formats: "2019-11-15T14:31:21Z", "2019-11-15T14:31:21+00:00"
  • Non-ISO8601 formats that DateTime.TryParse accepts: "11/15/2019", "Nov 15, 2019"
  • Strings that should remain as String type

This would document the new behavior comprehensively and ensure the change works as intended across different scenarios.

Copilot uses AI. Check for mistakes.

#endregion

#region DecryptJwtToken Tests
[Fact]
public void DecryptJwtToken_WhenValidationParametersIsNull_ThrowsException()
Expand Down
18 changes: 18 additions & 0 deletions test/System.IdentityModel.Tokens.Jwt.Tests/JwtPayloadTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Reflection;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
using Microsoft.IdentityModel.Tokens.Json;
Expand Down Expand Up @@ -182,6 +183,23 @@ public void TestDateTimeClaim()
Assert.True(string.Equals(dateTimeClaim.Value, dateTime.ToUniversalTime().ToString("o", CultureInfo.InvariantCulture)), "dateTimeClaim.Value != dateTime.ToUniversalTime('o', CultureInfo.InvariantCulture).ToString()");
}

[Fact]
public void TestReturnsDateTime_ForIso8601WithOrWithoutTrailingZeros()
{
var iso8601WithTrailingZeros = "2025-11-26T09:30:45.1234560Z";
var iso8601WithoutTrailingZeros = "2025-11-26T09:30:45.123456Z";
var jwtpayload = new JwtPayload
{
{ "dateWithTrailingZeros", iso8601WithTrailingZeros },
{ "dateWithoutTrailingZeros", iso8601WithoutTrailingZeros }
};

var claimWithTrailingZeros = jwtpayload.Claims.First(c => c.Type == "dateWithTrailingZeros");
var claimWithoutTrailingZeros = jwtpayload.Claims.First(c => c.Type == "dateWithoutTrailingZeros");
Assert.Equal(ClaimValueTypes.DateTime, claimWithTrailingZeros.ValueType);
Assert.Equal(ClaimValueTypes.DateTime, claimWithoutTrailingZeros.ValueType);
}
Comment on lines +186 to +201
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test cases for edge cases beyond trailing zeros, such as:

  • Date-only strings: "2019-11-15"
  • Various ISO8601 formats with different timezone indicators
  • Non-ISO8601 formats that DateTime.TryParse accepts
  • Strings that should remain as String type

This would document the new behavior comprehensively and ensure the change works as intended across different scenarios.

Copilot uses AI. Check for mistakes.

[Fact]
public void TestClaimWithLargeExpValue()
{
Expand Down