Skip to content

Commit 9d007d8

Browse files
authored
For NET 9 and above use Lock objects for locks (#3171)
1 parent f4d21c7 commit 9d007d8

File tree

9 files changed

+100
-16
lines changed

9 files changed

+100
-16
lines changed

src/Microsoft.IdentityModel.JsonWebTokens/Json/JsonClaimSet.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
using Microsoft.IdentityModel.Tokens;
1313
using Microsoft.IdentityModel.Tokens.Json;
1414

15+
#if NET9_0_OR_GREATER
16+
using System.Threading;
17+
#endif
18+
1519
namespace Microsoft.IdentityModel.JsonWebTokens
1620
{
1721
/// <summary>
@@ -21,8 +25,11 @@ namespace Microsoft.IdentityModel.JsonWebTokens
2125
internal class JsonClaimSet
2226
{
2327
internal const string ClassName = "Microsoft.IdentityModel.JsonWebTokens.JsonClaimSet";
24-
28+
#if NET9_0_OR_GREATER
29+
internal Lock _claimsLock = new();
30+
#else
2531
internal object _claimsLock = new();
32+
#endif
2633
internal readonly Dictionary<string, object> _jsonClaims;
2734
private List<Claim> _claims;
2835

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Microsoft.IdentityModel.JsonWebTokens.JsonClaimSet._claimsLock -> System.Threading.Lock

src/Microsoft.IdentityModel.Tokens/Encryption/SymmetricKeyWrapProvider.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
using System.Security.Cryptography;
66
using Microsoft.IdentityModel.Logging;
77

8+
#if NET9_0_OR_GREATER
9+
using System.Threading;
10+
#endif
11+
812
namespace Microsoft.IdentityModel.Tokens
913
{
1014
/// <summary>
@@ -15,9 +19,13 @@ public class SymmetricKeyWrapProvider : KeyWrapProvider
1519
private static readonly byte[] _defaultIV = new byte[] { 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6 };
1620
private const int _blockSizeInBits = 64;
1721
private const int _blockSizeInBytes = _blockSizeInBits >> 3;
18-
private static readonly object _encryptorLock = new object();
19-
private static readonly object _decryptorLock = new object();
20-
22+
#if NET9_0_OR_GREATER
23+
private static readonly Lock s_encryptorLock = new();
24+
private static readonly Lock s_decryptorLock = new();
25+
#else
26+
private static readonly object s_encryptorLock = new();
27+
private static readonly object s_decryptorLock = new();
28+
#endif
2129
private Lazy<SymmetricAlgorithm> _symmetricAlgorithm;
2230
private ICryptoTransform _symmetricAlgorithmEncryptor;
2331
private ICryptoTransform _symmetricAlgorithmDecryptor;
@@ -259,7 +267,7 @@ Return an error
259267

260268
if (_symmetricAlgorithmDecryptor == null)
261269
{
262-
lock (_decryptorLock)
270+
lock (s_decryptorLock)
263271
{
264272
if (_symmetricAlgorithmDecryptor == null)
265273
_symmetricAlgorithmDecryptor = _symmetricAlgorithm.Value.CreateDecryptor();
@@ -409,7 +417,7 @@ private byte[] WrapKeyPrivate(byte[] inputBuffer, int inputOffset, int inputCoun
409417

410418
if (_symmetricAlgorithmEncryptor == null)
411419
{
412-
lock (_encryptorLock)
420+
lock (s_encryptorLock)
413421
{
414422
if (_symmetricAlgorithmEncryptor == null)
415423
_symmetricAlgorithmEncryptor = _symmetricAlgorithm.Value.CreateEncryptor();

src/Microsoft.IdentityModel.Tokens/SecurityKey.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
using System.Text.Json.Serialization;
66
using Microsoft.IdentityModel.Logging;
77

8+
#if NET9_0_OR_GREATER
9+
using System.Threading;
10+
#endif
11+
812
namespace Microsoft.IdentityModel.Tokens
913
{
1014
/// <summary>
@@ -13,9 +17,13 @@ namespace Microsoft.IdentityModel.Tokens
1317
public abstract class SecurityKey
1418
{
1519
private CryptoProviderFactory _cryptoProviderFactory;
16-
private object _internalIdLock = new object();
17-
private string _internalId;
1820

21+
#if NET9_0_OR_GREATER
22+
private readonly Lock _internalIdLock = new();
23+
#else
24+
private readonly object _internalIdLock = new();
25+
#endif
26+
private string _internalId;
1927
internal SecurityKey(SecurityKey key)
2028
{
2129
_cryptoProviderFactory = key._cryptoProviderFactory;

src/Microsoft.IdentityModel.Tokens/Validation/Results/TokenValidationResult.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public class TokenValidationResult
3030
// reordered relative to the other operations. The rest of the objects are not because the .NET memory model
3131
// guarantees object writes are store releases and that reads won't be introduced.
3232
private volatile bool _claimsIdentityInitialized;
33+
#if NET9_0_OR_GREATER
34+
private Lock _claimsIdentitySyncObj;
35+
#else
3336
private object _claimsIdentitySyncObj;
37+
#endif
3438
private ClaimsIdentity _claimsIdentity;
3539
private Dictionary<string, object> _claims;
3640
private Dictionary<string, object> _propertyBag;
@@ -196,6 +200,23 @@ internal ClaimsIdentity ClaimsIdentityNoLocking
196200
}
197201
}
198202

203+
#if NET9_0_OR_GREATER
204+
/// <summary>Gets the object to use in <see cref="ClaimsIdentity"/> for double-checked locking.</summary>
205+
private Lock ClaimsIdentitySyncObj
206+
{
207+
get
208+
{
209+
Lock syncObj = _claimsIdentitySyncObj;
210+
if (syncObj is null)
211+
{
212+
Interlocked.CompareExchange(ref _claimsIdentitySyncObj, new Lock(), null);
213+
syncObj = _claimsIdentitySyncObj;
214+
}
215+
216+
return syncObj;
217+
}
218+
}
219+
#else
199220
/// <summary>Gets the object to use in <see cref="ClaimsIdentity"/> for double-checked locking.</summary>
200221
private object ClaimsIdentitySyncObj
201222
{
@@ -211,7 +232,7 @@ private object ClaimsIdentitySyncObj
211232
return syncObj;
212233
}
213234
}
214-
235+
#endif
215236
/// <summary>
216237
/// Gets or sets the <see cref="Exception"/> that occurred during validation.
217238
/// </summary>

src/Microsoft.IdentityModel.Tokens/Validation/Results/ValidatedToken.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ ActorValidationResult is not null
114114
// reordered relative to the other operations. The rest of the objects are not because the .NET memory model
115115
// guarantees object writes are store releases and that reads won't be introduced.
116116
private volatile bool _claimsIdentityInitialized;
117-
private object? _claimsIdentitySyncObj;
118117
private ClaimsIdentity? _claimsIdentity;
119118
private Dictionary<string, object>? _claims;
119+
#if NET9_0_OR_GREATER
120+
private Lock? _claimsIdentitySyncObj;
121+
#else
122+
private object? _claimsIdentitySyncObj;
123+
#endif
120124

121125
/// <summary>
122126
/// The <see cref="Dictionary{String, Object}"/> created from the validated security token.
@@ -190,6 +194,23 @@ internal ClaimsIdentity ClaimsIdentityNoLocking
190194
}
191195
}
192196

197+
#if NET9_0_OR_GREATER
198+
/// <summary>Gets the Lock to use in <see cref="ClaimsIdentity"/> for double-checked locking.</summary>
199+
private Lock ClaimsIdentitySyncObj
200+
{
201+
get
202+
{
203+
Lock? syncObj = _claimsIdentitySyncObj;
204+
if (syncObj is null)
205+
{
206+
Interlocked.CompareExchange(ref _claimsIdentitySyncObj, new Lock(), null);
207+
syncObj = _claimsIdentitySyncObj;
208+
}
209+
210+
return syncObj;
211+
}
212+
}
213+
#else
193214
/// <summary>Gets the object to use in <see cref="ClaimsIdentity"/> for double-checked locking.</summary>
194215
private object ClaimsIdentitySyncObj
195216
{
@@ -205,6 +226,7 @@ private object ClaimsIdentitySyncObj
205226
return syncObj;
206227
}
207228
}
229+
#endif
208230
#endregion
209231

210232
#region Logging

src/Microsoft.IdentityModel.Tokens/X509SecurityKey.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
using System.Security.Cryptography;
66
using System.Security.Cryptography.X509Certificates;
77
using Microsoft.IdentityModel.Logging;
8+
#if NET9_0_OR_GREATER
9+
using System.Threading;
10+
#endif
811

912
namespace Microsoft.IdentityModel.Tokens
1013
{
@@ -16,8 +19,11 @@ public class X509SecurityKey : AsymmetricSecurityKey
1619
AsymmetricAlgorithm _privateKey;
1720
bool _privateKeyAvailabilityDetermined;
1821
AsymmetricAlgorithm _publicKey;
19-
object _thisLock = new Object();
20-
22+
#if NET9_0_OR_GREATER
23+
Lock _thisLock = new();
24+
#else
25+
object _thisLock = new();
26+
#endif
2127
internal X509SecurityKey(JsonWebKey webKey)
2228
: base(webKey)
2329
{
@@ -110,12 +116,14 @@ public AsymmetricAlgorithm PublicKey
110116
return _publicKey;
111117
}
112118
}
113-
119+
#if NET9_0_OR_GREATER
120+
Lock ThisLock => _thisLock;
121+
#else
114122
object ThisLock
115123
{
116124
get { return _thisLock; }
117125
}
118-
126+
#endif
119127
/// <summary>
120128
/// Gets a bool indicating if a private key exists.
121129
/// </summary>

src/Microsoft.IdentityModel.Xml/EnvelopedSignatureWriter.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
using Microsoft.IdentityModel.Tokens;
99
using static Microsoft.IdentityModel.Logging.LogHelper;
1010

11+
#if NET9_0_OR_GREATER
12+
using System.Threading;
13+
#endif
14+
1115
namespace Microsoft.IdentityModel.Xml
1216
{
1317
/// <summary>
@@ -39,7 +43,12 @@ public class EnvelopedSignatureWriter : DelegatingXmlDictionaryWriter
3943
private bool _signaturePlaceholderWritten;
4044
private SigningCredentials _signingCredentials;
4145
private MemoryStream _internalStream;
42-
private object _signatureLock = new object();
46+
47+
#if NET9_0_OR_GREATER
48+
private Lock _signatureLock = new();
49+
#else
50+
private object _signatureLock = new();
51+
#endif
4352

4453
/// <summary>
4554
/// Initializes an instance of <see cref="EnvelopedSignatureWriter"/>. The returned writer can be directly used

test/Microsoft.IdentityModel.Abstractions.Tests/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright(c) Microsoft Corporation.All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License.
33

44
using System;

0 commit comments

Comments
 (0)