Skip to content

#62774 Fix JWT Bearer unit tests failing if local timezone is UTC+N #62775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

navferty
Copy link
Contributor

Tests were failing if local timezone is to the east of UTC, due to DateTime.MaxValue is locale-specific. Add explicit DateTimeKind UTC and immediate conversion to UTC when parsing, since this time is unrepresentable in local timezone

Fixes #62774

…TC+N

Tests were failing if local timezone is to the east of UTC, due to DateTime.MaxValue is locale-specific. Add explicit DateTimeKind UTC and immediate conversion to UTC when parsing, since this time is unrepresentable in local timezone
@Copilot Copilot AI review requested due to automatic review settings July 17, 2025 13:58
@navferty navferty requested a review from halter73 as a code owner July 17, 2025 13:58
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 17, 2025
Copy link
Contributor

Thanks for your PR, @@navferty. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@github-actions github-actions bot added the area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer label Jul 17, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes JWT Bearer unit tests that were failing when run in timezones east of UTC (UTC+N). The issue occurred because DateTime.MaxValue is locale-specific, causing test failures in certain timezone configurations.

Key changes:

  • Explicitly set DateTimeKind.Utc when creating JWT tokens with DateTime.MaxValue expiration
  • Simplified DateTime parsing to directly convert to UTC using DateTimeStyles.AdjustToUniversal

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Security/Authentication/test/JwtBearerTests_Handler.cs Updated JWT token creation and DateTime parsing to use explicit UTC handling
src/Security/Authentication/test/JwtBearerTests.cs Applied identical fixes for JWT token creation and DateTime parsing consistency

@@ -944,8 +944,7 @@ public async Task ExpirationAndIssuedWhenMinOrMaxValue()
var expiresElement = dom.RootElement.GetProperty("expires");
Assert.Equal(JsonValueKind.String, expiresElement.ValueKind);

var elementValue = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture);
var elementValueUtc = elementValue.ToUniversalTime();
var elementValueUtc = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

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

Consider using DateTime.ParseExact or DateTimeOffset.Parse for more robust parsing of JWT timestamps, as JWT standards typically use ISO 8601 format. This would be more explicit about the expected format and reduce ambiguity.

Suggested change
var elementValueUtc = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
var elementValueUtc = DateTime.ParseExact(expiresElement.GetString(), "yyyy-MM-ddTHH:mm:ss.fffffffK", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

Copilot uses AI. Check for mistakes.

@@ -1000,8 +1000,7 @@ public async Task ExpirationAndIssuedWhenMinOrMaxValue()
var expiresElement = dom.RootElement.GetProperty("expires");
Assert.Equal(JsonValueKind.String, expiresElement.ValueKind);

var elementValue = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture);
var elementValueUtc = elementValue.ToUniversalTime();
var elementValueUtc = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
Copy link
Preview

Copilot AI Jul 17, 2025

Choose a reason for hiding this comment

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

Consider using DateTime.ParseExact or DateTimeOffset.Parse for more robust parsing of JWT timestamps, as JWT standards typically use ISO 8601 format. This would be more explicit about the expected format and reduce ambiguity.

Suggested change
var elementValueUtc = DateTime.Parse(expiresElement.GetString(), CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
var elementValueUtc = DateTime.ParseExact(expiresElement.GetString(), "o", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Some Jwt Bearer unit tests are failing in specific local time zones
1 participant
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy