10
10
using System . Text . RegularExpressions ;
11
11
using System . Threading ;
12
12
using System . Threading . Tasks ;
13
+ using Microsoft . IdentityModel . Abstractions ;
13
14
using Microsoft . IdentityModel . Json ;
14
15
using Microsoft . IdentityModel . Json . Linq ;
15
16
using Microsoft . IdentityModel . Logging ;
@@ -159,7 +160,9 @@ public virtual bool CanReadToken(string token)
159
160
160
161
if ( token . Length > MaximumTokenSizeInBytes )
161
162
{
162
- LogHelper . LogInformation ( TokenLogMessages . IDX10209 , LogHelper . MarkAsNonPII ( token . Length ) , LogHelper . MarkAsNonPII ( MaximumTokenSizeInBytes ) ) ;
163
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
164
+ LogHelper . LogInformation ( TokenLogMessages . IDX10209 , LogHelper . MarkAsNonPII ( token . Length ) , LogHelper . MarkAsNonPII ( MaximumTokenSizeInBytes ) ) ;
165
+
163
166
return false ;
164
167
}
165
168
@@ -327,9 +330,12 @@ public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor)
327
330
if ( tokenDescriptor == null )
328
331
throw LogHelper . LogArgumentNullException ( nameof ( tokenDescriptor ) ) ;
329
332
330
- if ( ( tokenDescriptor . Subject == null || ! tokenDescriptor . Subject . Claims . Any ( ) )
331
- && ( tokenDescriptor . Claims == null || ! tokenDescriptor . Claims . Any ( ) ) )
332
- LogHelper . LogWarning ( LogMessages . IDX14114 , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Subject ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Claims ) ) ) ;
333
+ if ( LogHelper . IsEnabled ( EventLogLevel . Warning ) )
334
+ {
335
+ if ( ( tokenDescriptor . Subject == null || ! tokenDescriptor . Subject . Claims . Any ( ) )
336
+ && ( tokenDescriptor . Claims == null || ! tokenDescriptor . Claims . Any ( ) ) )
337
+ LogHelper . LogWarning ( LogMessages . IDX14114 , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Subject ) ) , LogHelper . MarkAsNonPII ( nameof ( SecurityTokenDescriptor . Claims ) ) ) ;
338
+ }
333
339
334
340
JObject payload ;
335
341
if ( tokenDescriptor . Subject != null )
@@ -344,7 +350,7 @@ public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor)
344
350
345
351
if ( tokenDescriptor . Audience != null )
346
352
{
347
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Aud ) )
353
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Aud ) )
348
354
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . Audience ) ) ) ) ;
349
355
350
356
payload [ JwtRegisteredClaimNames . Aud ] = tokenDescriptor . Audience ;
@@ -360,23 +366,23 @@ public virtual string CreateToken(SecurityTokenDescriptor tokenDescriptor)
360
366
361
367
if ( tokenDescriptor . Issuer != null )
362
368
{
363
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Iss ) )
369
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Iss ) )
364
370
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . Issuer ) ) ) ) ;
365
371
366
372
payload [ JwtRegisteredClaimNames . Iss ] = tokenDescriptor . Issuer ;
367
373
}
368
374
369
375
if ( tokenDescriptor . IssuedAt . HasValue )
370
376
{
371
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Iat ) )
377
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Iat ) )
372
378
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . IssuedAt ) ) ) ) ;
373
379
374
380
payload [ JwtRegisteredClaimNames . Iat ] = EpochTime . GetIntDate ( tokenDescriptor . IssuedAt . Value ) ;
375
381
}
376
382
377
383
if ( tokenDescriptor . NotBefore . HasValue )
378
384
{
379
- if ( payload . ContainsKey ( JwtRegisteredClaimNames . Nbf ) )
385
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) && payload . ContainsKey ( JwtRegisteredClaimNames . Nbf ) )
380
386
LogHelper . LogInformation ( LogHelper . FormatInvariant ( LogMessages . IDX14113 , LogHelper . MarkAsNonPII ( nameof ( tokenDescriptor . NotBefore ) ) ) ) ;
381
387
382
388
payload [ JwtRegisteredClaimNames . Nbf ] = EpochTime . GetIntDate ( tokenDescriptor . NotBefore . Value ) ;
@@ -688,7 +694,8 @@ private string CreateTokenPrivate(
688
694
}
689
695
catch ( Exception ex )
690
696
{
691
- LogHelper . LogExceptionMessage ( new SecurityTokenException ( LogHelper . FormatInvariant ( LogMessages . IDX14307 , ex , payload ) ) ) ;
697
+ if ( LogHelper . IsEnabled ( EventLogLevel . Error ) )
698
+ LogHelper . LogExceptionMessage ( new SecurityTokenException ( LogHelper . FormatInvariant ( LogMessages . IDX14307 , ex , payload ) ) ) ;
692
699
}
693
700
694
701
payload = jsonPayload != null ? jsonPayload . ToString ( Formatting . None ) : payload ;
@@ -831,7 +838,9 @@ private static string GetActualIssuer(JsonWebToken jwtToken)
831
838
string actualIssuer = jwtToken . Issuer ;
832
839
if ( string . IsNullOrWhiteSpace ( actualIssuer ) )
833
840
{
834
- LogHelper . LogVerbose ( TokenLogMessages . IDX10244 , ClaimsIdentity . DefaultIssuer ) ;
841
+ if ( LogHelper . IsEnabled ( EventLogLevel . Verbose ) )
842
+ LogHelper . LogVerbose ( TokenLogMessages . IDX10244 , ClaimsIdentity . DefaultIssuer ) ;
843
+
835
844
actualIssuer = ClaimsIdentity . DefaultIssuer ;
836
845
}
837
846
@@ -1126,12 +1135,13 @@ internal IEnumerable<SecurityKey> GetContentEncryptionKeys(JsonWebToken jwtToken
1126
1135
var key = ResolveTokenDecryptionKey ( jwtToken . EncodedToken , jwtToken , validationParameters ) ;
1127
1136
if ( key != null )
1128
1137
{
1129
- LogHelper . LogInformation ( TokenLogMessages . IDX10904 , key ) ;
1138
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1139
+ LogHelper . LogInformation ( TokenLogMessages . IDX10904 , key ) ;
1130
1140
}
1131
1141
else if ( configuration != null )
1132
1142
{
1133
1143
key = ResolveTokenDecryptionKeyFromConfig ( jwtToken , configuration ) ;
1134
- if ( key != null )
1144
+ if ( key != null && LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1135
1145
LogHelper . LogInformation ( TokenLogMessages . IDX10905 , key ) ;
1136
1146
}
1137
1147
@@ -1455,7 +1465,8 @@ private async Task<TokenValidationResult> ValidateTokenAsync(JsonWebToken jsonWe
1455
1465
{
1456
1466
// The exception is not re-thrown as the TokenValidationParameters may have the issuer and signing key set
1457
1467
// directly on them, allowing the library to continue with token validation.
1458
- LogHelper . LogWarning ( LogHelper . FormatInvariant ( TokenLogMessages . IDX10261 , validationParameters . ConfigurationManager . MetadataAddress , ex . ToString ( ) ) ) ;
1468
+ if ( LogHelper . IsEnabled ( EventLogLevel . Warning ) )
1469
+ LogHelper . LogWarning ( LogHelper . FormatInvariant ( TokenLogMessages . IDX10261 , validationParameters . ConfigurationManager . MetadataAddress , ex . ToString ( ) ) ) ;
1459
1470
}
1460
1471
}
1461
1472
@@ -1731,7 +1742,9 @@ private static JsonWebToken ValidateSignature(JsonWebToken jwtToken, TokenValida
1731
1742
{
1732
1743
if ( ValidateSignature ( jwtToken , key , validationParameters ) )
1733
1744
{
1734
- LogHelper . LogInformation ( TokenLogMessages . IDX10242 , jwtToken ) ;
1745
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1746
+ LogHelper . LogInformation ( TokenLogMessages . IDX10242 , jwtToken ) ;
1747
+
1735
1748
jwtToken . SigningKey = key ;
1736
1749
return jwtToken ;
1737
1750
}
@@ -1813,7 +1826,9 @@ internal static bool ValidateSignature(byte[] encodedBytes, byte[] signature, Se
1813
1826
var cryptoProviderFactory = validationParameters . CryptoProviderFactory ?? key . CryptoProviderFactory ;
1814
1827
if ( ! cryptoProviderFactory . IsSupportedAlgorithm ( algorithm , key ) )
1815
1828
{
1816
- LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( algorithm ) , key ) ;
1829
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1830
+ LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( algorithm ) , key ) ;
1831
+
1817
1832
return false ;
1818
1833
}
1819
1834
@@ -1871,7 +1886,9 @@ internal static bool ValidateSignature(JsonWebToken jsonWebToken, SecurityKey ke
1871
1886
var cryptoProviderFactory = validationParameters . CryptoProviderFactory ?? key . CryptoProviderFactory ;
1872
1887
if ( ! cryptoProviderFactory . IsSupportedAlgorithm ( jsonWebToken . Alg , key ) )
1873
1888
{
1874
- LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( jsonWebToken . Alg ) , key ) ;
1889
+ if ( LogHelper . IsEnabled ( EventLogLevel . Informational ) )
1890
+ LogHelper . LogInformation ( LogMessages . IDX14000 , LogHelper . MarkAsNonPII ( jsonWebToken . Alg ) , key ) ;
1891
+
1875
1892
return false ;
1876
1893
}
1877
1894
0 commit comments