@@ -2164,7 +2164,8 @@ public void ConfidentialClient_WithInvalidAuthority_ThrowsArgumentException()
21642164 [ TestMethod ]
21652165 public async Task WithAccessTokenSha256ToRefresh_MatchingHash_GetsTokenFromIdp_Async ( )
21662166 {
2167- const string accessToken = "access-token" ;
2167+ const string accessToken = "test_token" ;
2168+ const string accessTokenHash = "cc0af97287543b65da2c7e1476426021826cab166f1e063ed012b855ff819656" ;
21682169
21692170 // Arrange
21702171 using ( var httpManager = new MockHttpManager ( ) )
@@ -2190,14 +2191,12 @@ public async Task WithAccessTokenSha256ToRefresh_MatchingHash_GetsTokenFromIdp_A
21902191 Assert . AreEqual ( TokenSource . Cache , secondResult . AuthenticationResultMetadata . TokenSource ) ;
21912192
21922193 // 3) Now specify the same token's hash as "bad" => expect a new token from IdP
2193- string tokenHash = ComputeSHA256 ( accessToken ) ;
2194-
21952194 // Add another network response to simulate fetching a new token
21962195 httpManager . AddMockHandlerSuccessfulClientCredentialTokenResponseMessage ( token : "new-access-token" ) ;
21972196
21982197 // Act: Use matching hash => triggers new token request
21992198 AuthenticationResult result = await app . AcquireTokenForClient ( TestConstants . s_scope )
2200- . WithAccessTokenSha256ToRefresh ( tokenHash )
2199+ . WithAccessTokenSha256ToRefresh ( accessTokenHash )
22012200 . ExecuteAsync ( )
22022201 . ConfigureAwait ( false ) ;
22032202
@@ -2231,7 +2230,7 @@ public async Task WithAccessTokenSha256ToRefresh_MismatchedHash_UsesCache_Async(
22312230
22322231 // 2) Mismatched hash => we expect to keep using the cached token
22332232 // Act
2234- var mismatchHash = ComputeSHA256 ( "some-other-token" ) ;
2233+ var mismatchHash = ComputeSHA256Hex ( "some-other-token" ) ;
22352234 AuthenticationResult result = await app . AcquireTokenForClient ( TestConstants . s_scope )
22362235 . WithAccessTokenSha256ToRefresh ( mismatchHash )
22372236 . ExecuteAsync ( )
@@ -2289,7 +2288,7 @@ public async Task AcquireTokenForClient_WithClaims_And_MatchingHash_SkipsCache_A
22892288 Assert . AreEqual ( oldToken , firstResult . AccessToken ) ;
22902289
22912290 // 2) We do matching hash => a new token is returned
2292- string tokenHash = ComputeSHA256 ( oldToken ) ;
2291+ string tokenHash = ComputeSHA256Hex ( oldToken ) ;
22932292
22942293 // Add second network response for the new token
22952294 httpManager . AddMockHandlerSuccessfulClientCredentialTokenResponseMessage ( token : freshToken ) ;
@@ -2334,7 +2333,7 @@ public async Task AcquireTokenForClient_WithClaims_And_MismatchedHash_UsesCache_
23342333 Assert . AreEqual ( TokenSource . IdentityProvider , initialResult . AuthenticationResultMetadata . TokenSource ) ;
23352334
23362335 // 2) We'll do a mismatched hash => expect to keep using the cached token
2337- string mismatchedHash = ComputeSHA256 ( "some-other-token" ) ;
2336+ string mismatchedHash = ComputeSHA256Hex ( "some-other-token" ) ;
23382337
23392338 // Act
23402339 var result = await app . AcquireTokenForClient ( TestConstants . s_scope )
@@ -2350,18 +2349,10 @@ public async Task AcquireTokenForClient_WithClaims_And_MismatchedHash_UsesCache_
23502349 }
23512350 }
23522351
2353- private static string ComputeSHA256 ( string token )
2352+ private static string ComputeSHA256Hex ( string token )
23542353 {
2355- #if NET6_0_OR_GREATER
2356- byte [ ] hashBytes = SHA256 . HashData ( Encoding . UTF8 . GetBytes ( token ) ) ;
2357- return Convert . ToBase64String ( hashBytes ) ;
2358- #else
2359- using ( var sha256 = SHA256 . Create ( ) )
2360- {
2361- byte [ ] hashBytes = sha256 . ComputeHash ( Encoding . UTF8 . GetBytes ( token ) ) ;
2362- return Convert . ToBase64String ( hashBytes ) ;
2363- }
2364- #endif
2354+ var cryptoMgr = new CommonCryptographyManager ( ) ;
2355+ return cryptoMgr . CreateSha256HashHex ( token ) ;
23652356 }
23662357 }
23672358}
0 commit comments