@@ -2164,7 +2164,8 @@ public void ConfidentialClient_WithInvalidAuthority_ThrowsArgumentException()
2164
2164
[ TestMethod ]
2165
2165
public async Task WithAccessTokenSha256ToRefresh_MatchingHash_GetsTokenFromIdp_Async ( )
2166
2166
{
2167
- const string accessToken = "access-token" ;
2167
+ const string accessToken = "test_token" ;
2168
+ const string accessTokenHash = "cc0af97287543b65da2c7e1476426021826cab166f1e063ed012b855ff819656" ;
2168
2169
2169
2170
// Arrange
2170
2171
using ( var httpManager = new MockHttpManager ( ) )
@@ -2190,14 +2191,12 @@ public async Task WithAccessTokenSha256ToRefresh_MatchingHash_GetsTokenFromIdp_A
2190
2191
Assert . AreEqual ( TokenSource . Cache , secondResult . AuthenticationResultMetadata . TokenSource ) ;
2191
2192
2192
2193
// 3) Now specify the same token's hash as "bad" => expect a new token from IdP
2193
- string tokenHash = ComputeSHA256 ( accessToken ) ;
2194
-
2195
2194
// Add another network response to simulate fetching a new token
2196
2195
httpManager . AddMockHandlerSuccessfulClientCredentialTokenResponseMessage ( token : "new-access-token" ) ;
2197
2196
2198
2197
// Act: Use matching hash => triggers new token request
2199
2198
AuthenticationResult result = await app . AcquireTokenForClient ( TestConstants . s_scope )
2200
- . WithAccessTokenSha256ToRefresh ( tokenHash )
2199
+ . WithAccessTokenSha256ToRefresh ( accessTokenHash )
2201
2200
. ExecuteAsync ( )
2202
2201
. ConfigureAwait ( false ) ;
2203
2202
@@ -2231,7 +2230,7 @@ public async Task WithAccessTokenSha256ToRefresh_MismatchedHash_UsesCache_Async(
2231
2230
2232
2231
// 2) Mismatched hash => we expect to keep using the cached token
2233
2232
// Act
2234
- var mismatchHash = ComputeSHA256 ( "some-other-token" ) ;
2233
+ var mismatchHash = ComputeSHA256Hex ( "some-other-token" ) ;
2235
2234
AuthenticationResult result = await app . AcquireTokenForClient ( TestConstants . s_scope )
2236
2235
. WithAccessTokenSha256ToRefresh ( mismatchHash )
2237
2236
. ExecuteAsync ( )
@@ -2289,7 +2288,7 @@ public async Task AcquireTokenForClient_WithClaims_And_MatchingHash_SkipsCache_A
2289
2288
Assert . AreEqual ( oldToken , firstResult . AccessToken ) ;
2290
2289
2291
2290
// 2) We do matching hash => a new token is returned
2292
- string tokenHash = ComputeSHA256 ( oldToken ) ;
2291
+ string tokenHash = ComputeSHA256Hex ( oldToken ) ;
2293
2292
2294
2293
// Add second network response for the new token
2295
2294
httpManager . AddMockHandlerSuccessfulClientCredentialTokenResponseMessage ( token : freshToken ) ;
@@ -2334,7 +2333,7 @@ public async Task AcquireTokenForClient_WithClaims_And_MismatchedHash_UsesCache_
2334
2333
Assert . AreEqual ( TokenSource . IdentityProvider , initialResult . AuthenticationResultMetadata . TokenSource ) ;
2335
2334
2336
2335
// 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" ) ;
2338
2337
2339
2338
// Act
2340
2339
var result = await app . AcquireTokenForClient ( TestConstants . s_scope )
@@ -2350,18 +2349,10 @@ public async Task AcquireTokenForClient_WithClaims_And_MismatchedHash_UsesCache_
2350
2349
}
2351
2350
}
2352
2351
2353
- private static string ComputeSHA256 ( string token )
2352
+ private static string ComputeSHA256Hex ( string token )
2354
2353
{
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 ) ;
2365
2356
}
2366
2357
}
2367
2358
}
0 commit comments