Repository navigation
Add support for TimeProvider in DataProtectionTokenProvider and DataProtectionTokenProviderOptions - #66586
Add support for TimeProvider in DataProtectionTokenProvider and DataProtectionTokenProviderOptions#66586marcominerva wants to merge 4 commits into
Conversation
…rotectionTokenProviderOptions
|
Thanks for your PR, @marcominerva. Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
There was a problem hiding this comment.
Pull request overview
Adds TimeProvider support to Identity's data-protection token provider so token generation/validation can use injectable time sources instead of DateTimeOffset.UtcNow, primarily to improve testability and align with existing Identity time abstractions.
Changes:
- Added
TimeProvidertoDataProtectionTokenProviderOptionsand exposed the resolved provider onDataProtectorTokenProvider<TUser>. - Updated token generation/validation to read current time from
TimeProvider. - Added DI post-configuration and new tests for option-based and DI-based time injection.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
src/Identity/test/Identity.Test/DataProtectorTokenProviderTest.cs |
Adds unit tests for token generation/validation and TimeProvider injection behavior. |
src/Identity/Core/src/PublicAPI.Unshipped.txt |
Records the new public API surface for TimeProvider. |
src/Identity/Core/src/IdentityServiceCollectionExtensions.cs |
Registers post-configuration for DataProtectionTokenProviderOptions in the AddIdentity path. |
src/Identity/Core/src/IdentityBuilderExtensions.cs |
Registers post-configuration for DataProtectionTokenProviderOptions in the default token-provider path. |
src/Identity/Core/src/DataProtectorTokenProvider.cs |
Switches token timestamp generation/validation to use TimeProvider. |
src/Identity/Core/src/DataProtectionTokenProviderOptions.cs |
Adds the new nullable TimeProvider option. |
…ions and add unit test for DI injection
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
…rovider # Conflicts: # src/Identity/Core/src/IdentityServiceCollectionExtensions.cs # src/Identity/Core/src/PublicAPI.Unshipped.txt
This pull request enhances the
DataProtectorTokenProviderin ASP.NET Core Identity by introducing support forTimeProvider, enabling improved testability and flexibility for time-dependent operations like token generation and validation. It also ensures that theTimeProvidercan be injected via dependency injection (DI) or set directly in options, and adds a comprehensive suite of tests to verify these behaviors. This changes uses the same pattern already applied in theSecurityStampValidatorclass.Key changes:
TimeProvider support and integration
TimeProviderproperty toDataProtectionTokenProviderOptionsto allow overriding the system clock, primarily for testing scenarios.DataProtectorTokenProviderto use the configuredTimeProvider(falling back toTimeProvider.Systemif not set) for all time-based operations, replacing direct usage ofDateTimeOffset.UtcNow. [1] [2] [3] [4]PostConfigureDataProtectionTokenProviderOptionsclass to set theTimeProviderfrom DI if not already set, mirroring the pattern used forSecurityStampValidatorOptions. This ensures consistent configuration via DI. [1] [2] [3] [4]Testing improvements
DataProtectorTokenProviderTest.cswith comprehensive tests covering token generation/validation, token expiry based on customTimeProvider, and correct precedence between DI-injected and manually setTimeProvider.Public API updates
TimeProviderproperties and accessors for bothDataProtectionTokenProviderOptionsandDataProtectorTokenProvider<TUser>.Fixes #54639