Skip to content

Commit b403fc1

Browse files
authored
Revert "For NET 9 and above use Lock objects for locks (#3171)"
This reverts commit 9d007d8.
1 parent c2bb66d commit b403fc1

9 files changed

Lines changed: 16 additions & 100 deletions

File tree

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
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-
1915
namespace Microsoft.IdentityModel.JsonWebTokens
2016
{
2117
/// <summary>
@@ -25,11 +21,8 @@ namespace Microsoft.IdentityModel.JsonWebTokens
2521
internal class JsonClaimSet
2622
{
2723
internal const string ClassName = "Microsoft.IdentityModel.JsonWebTokens.JsonClaimSet";
28-
#if NET9_0_OR_GREATER
29-
internal Lock _claimsLock = new();
30-
#else
24+
3125
internal object _claimsLock = new();
32-
#endif
3326
internal readonly Dictionary<string, object> _jsonClaims;
3427
private List<Claim> _claims;
3528

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

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

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

8-
#if NET9_0_OR_GREATER
9-
using System.Threading;
10-
#endif
11-
128
namespace Microsoft.IdentityModel.Tokens
139
{
1410
/// <summary>
@@ -19,13 +15,9 @@ public class SymmetricKeyWrapProvider : KeyWrapProvider
1915
private static readonly byte[] _defaultIV = new byte[] { 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6, 0xA6 };
2016
private const int _blockSizeInBits = 64;
2117
private const int _blockSizeInBytes = _blockSizeInBits >> 3;
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
18+
private static readonly object _encryptorLock = new object();
19+
private static readonly object _decryptorLock = new object();
20+
2921
private Lazy<SymmetricAlgorithm> _symmetricAlgorithm;
3022
private ICryptoTransform _symmetricAlgorithmEncryptor;
3123
private ICryptoTransform _symmetricAlgorithmDecryptor;
@@ -267,7 +259,7 @@ Return an error
267259

268260
if (_symmetricAlgorithmDecryptor == null)
269261
{
270-
lock (s_decryptorLock)
262+
lock (_decryptorLock)
271263
{
272264
if (_symmetricAlgorithmDecryptor == null)
273265
_symmetricAlgorithmDecryptor = _symmetricAlgorithm.Value.CreateDecryptor();
@@ -417,7 +409,7 @@ private byte[] WrapKeyPrivate(byte[] inputBuffer, int inputOffset, int inputCoun
417409

418410
if (_symmetricAlgorithmEncryptor == null)
419411
{
420-
lock (s_encryptorLock)
412+
lock (_encryptorLock)
421413
{
422414
if (_symmetricAlgorithmEncryptor == null)
423415
_symmetricAlgorithmEncryptor = _symmetricAlgorithm.Value.CreateEncryptor();

src/Microsoft.IdentityModel.Tokens/SecurityKey.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
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-
128
namespace Microsoft.IdentityModel.Tokens
139
{
1410
/// <summary>
@@ -17,13 +13,9 @@ namespace Microsoft.IdentityModel.Tokens
1713
public abstract class SecurityKey
1814
{
1915
private CryptoProviderFactory _cryptoProviderFactory;
20-
21-
#if NET9_0_OR_GREATER
22-
private readonly Lock _internalIdLock = new();
23-
#else
24-
private readonly object _internalIdLock = new();
25-
#endif
16+
private object _internalIdLock = new object();
2617
private string _internalId;
18+
2719
internal SecurityKey(SecurityKey key)
2820
{
2921
_cryptoProviderFactory = key._cryptoProviderFactory;

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,7 @@ 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
3633
private object _claimsIdentitySyncObj;
37-
#endif
3834
private ClaimsIdentity _claimsIdentity;
3935
private Dictionary<string, object> _claims;
4036
private Dictionary<string, object> _propertyBag;
@@ -200,23 +196,6 @@ internal ClaimsIdentity ClaimsIdentityNoLocking
200196
}
201197
}
202198

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
220199
/// <summary>Gets the object to use in <see cref="ClaimsIdentity"/> for double-checked locking.</summary>
221200
private object ClaimsIdentitySyncObj
222201
{
@@ -232,7 +211,7 @@ private object ClaimsIdentitySyncObj
232211
return syncObj;
233212
}
234213
}
235-
#endif
214+
236215
/// <summary>
237216
/// Gets or sets the <see cref="Exception"/> that occurred during validation.
238217
/// </summary>

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,9 @@ 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;
117118
private ClaimsIdentity? _claimsIdentity;
118119
private Dictionary<string, object>? _claims;
119-
#if NET9_0_OR_GREATER
120-
private Lock? _claimsIdentitySyncObj;
121-
#else
122-
private object? _claimsIdentitySyncObj;
123-
#endif
124120

125121
/// <summary>
126122
/// The <see cref="Dictionary{String, Object}"/> created from the validated security token.
@@ -194,23 +190,6 @@ internal ClaimsIdentity ClaimsIdentityNoLocking
194190
}
195191
}
196192

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
214193
/// <summary>Gets the object to use in <see cref="ClaimsIdentity"/> for double-checked locking.</summary>
215194
private object ClaimsIdentitySyncObj
216195
{
@@ -226,7 +205,6 @@ private object ClaimsIdentitySyncObj
226205
return syncObj;
227206
}
228207
}
229-
#endif
230208
#endregion
231209

232210
#region Logging

src/Microsoft.IdentityModel.Tokens/X509SecurityKey.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
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
118

129
namespace Microsoft.IdentityModel.Tokens
1310
{
@@ -19,11 +16,8 @@ public class X509SecurityKey : AsymmetricSecurityKey
1916
AsymmetricAlgorithm _privateKey;
2017
bool _privateKeyAvailabilityDetermined;
2118
AsymmetricAlgorithm _publicKey;
22-
#if NET9_0_OR_GREATER
23-
Lock _thisLock = new();
24-
#else
25-
object _thisLock = new();
26-
#endif
19+
object _thisLock = new Object();
20+
2721
internal X509SecurityKey(JsonWebKey webKey)
2822
: base(webKey)
2923
{
@@ -116,14 +110,12 @@ public AsymmetricAlgorithm PublicKey
116110
return _publicKey;
117111
}
118112
}
119-
#if NET9_0_OR_GREATER
120-
Lock ThisLock => _thisLock;
121-
#else
113+
122114
object ThisLock
123115
{
124116
get { return _thisLock; }
125117
}
126-
#endif
118+
127119
/// <summary>
128120
/// Gets a bool indicating if a private key exists.
129121
/// </summary>

src/Microsoft.IdentityModel.Xml/EnvelopedSignatureWriter.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
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-
1511
namespace Microsoft.IdentityModel.Xml
1612
{
1713
/// <summary>
@@ -43,12 +39,7 @@ public class EnvelopedSignatureWriter : DelegatingXmlDictionaryWriter
4339
private bool _signaturePlaceholderWritten;
4440
private SigningCredentials _signingCredentials;
4541
private MemoryStream _internalStream;
46-
47-
#if NET9_0_OR_GREATER
48-
private Lock _signatureLock = new();
49-
#else
50-
private object _signatureLock = new();
51-
#endif
42+
private object _signatureLock = new object();
5243

5344
/// <summary>
5445
/// 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)