Skip to content

Commit f8aabfa

Browse files
authored
Remove IDisposable from TokenCache (Azure#18346)
* Remove IDisposable from TokenCache
1 parent 7cf8349 commit f8aabfa

File tree

3 files changed

+48
-54
lines changed

3 files changed

+48
-54
lines changed

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,34 @@
22

33
## 1.4.0-beta.3 (Unreleased)
44

5+
### Breaking Changes
6+
7+
- The `IDisposable` interface has been removed from `TokenCache`.
58

69
## 1.4.0-beta.2 (2021-01-29)
710

811
### Fixes and improvements
12+
913
- Fixed issue in `HttpExtensions` causing the omission of content headers on requests ([#17448](https://github.com/Azure/azure-sdk-for-net/issues/17448))
1014
- Made `EnvironmentCredential` to account for both null and empty string when checking for the presense of the environment variables ([#18031](https://github.com/Azure/azure-sdk-for-net/issues/18031))
1115

1216
## 1.4.0-beta.1 (2020-10-15)
1317

1418
### New Features
19+
1520
- Redesigned Application Authentication APIs
1621
- Adds `TokenCache` and `PersistentTokenCache` classes to give more user control over how the tokens are cached and how the cache is persisted.
1722
- Adds `TokenCache` property to options for credentials supporting token cache configuration.
1823

1924
## 1.3.0 (2020-11-12)
2025

2126
### New Features
27+
2228
- Added support for Service Fabric managed identity authentication to `ManagedIdentityCredential`.
2329
- Added support for Azure Arc managed identity authentication to `ManagedIdentityCredential`.
2430

2531
### Fixes and improvements
32+
2633
- Fix race condition in `ProcessRunner` causing `VisualStudioCredential` and `AzureCliCredential` to fail intermittently ([#16211](https://github.com/Azure/azure-sdk-for-net/issues/16211))
2734
- Fix `VisualStudioCodeCredential` to raise `CredentialUnavailableException` when reading from VS Code's stored secret ([#16795](https://github.com/Azure/azure-sdk-for-net/issues/16795))
2835
- Prevent `VisualStudioCodeCredential` using invalid authentication data when no user is signed in to Visual Studio Code ([#15870](https://github.com/Azure/azure-sdk-for-net/issues/15870))
@@ -32,6 +39,7 @@
3239
- Fix cache loading issue in `SharedTokenCacheCredential` on Linux ([#12939](https://github.com/Azure/azure-sdk-for-net/issues/12939))
3340

3441
### Breaking Changes
42+
3543
- Rename property `IncludeX5CCliamHeader` on `ClientCertificateCredentialOptions` to `SendCertificateChain`
3644
- Removing Application Authentication APIs for GA release. These will be reintroduced in 1.4.0-beta.1.
3745
- Removed class `AuthenticationRecord`
@@ -46,16 +54,19 @@
4654
## 1.3.0-beta.2 (2020-10-07)
4755

4856
### New Features
57+
4958
- Update `DeviceCodeCredential` to output device code information and authentication instructions in the console, in the case no `deviceCodeCallback` is specified.
5059
- Added `DeviceCodeCallback` to `DeviceCodeCredentialOptions`
5160
- Added default constructor to `DeviceCodeCredential`
5261

5362
### Breaking Changes
63+
5464
- Replaced `DeviceCodeCredential` constructor overload taking `deviceCodeCallback` and `DeviceCodeCredentialOptions` with constructor taking only `DeviceCodeCredentialOptions`
5565

5666
## 1.3.0-beta.1 (2020-09-11)
5767

5868
### New Features
69+
5970
- Restoring Application Authentication APIs from 1.2.0-preview.6
6071
- Added support for App Service Managed Identity API version `2019-08-01` ([#13687](https://github.com/Azure/azure-sdk-for-net/issues/13687))
6172
- Added `IncludeX5CClaimHeader` to `ClientCertificateCredentialOptions` to enable subject name / issuer authentication with the `ClientCertificateCredential`.
@@ -64,6 +75,7 @@
6475
- Unify exception handling between `DefaultAzureCredential` and `ChainedTokenCredential` ([#14408](https://github.com/Azure/azure-sdk-for-net/issues/14408))
6576

6677
### Fixes and improvements
78+
6779
- Updated `MsalPublicClient` and `MsalConfidentialClient` to respect `CancellationToken` during initialization ([#13201](https://github.com/Azure/azure-sdk-for-net/issues/13201))
6880
- Fixed `VisualStudioCodeCredential` crashes on macOS (Issue [#14362](https://github.com/Azure/azure-sdk-for-net/issues/14362))
6981
- Fixed issue with non GUID Client Ids (Issue [#14585](https://github.com/Azure/azure-sdk-for-net/issues/14585))
@@ -72,22 +84,26 @@
7284
## 1.2.3 (2020-09-11)
7385

7486
### Fixes and improvements
87+
7588
- Fixed issue with `DefaultAzureCredential` incorrectly catching `AuthenticationFailedException` (Issue [#14974](https://github.com/Azure/azure-sdk-for-net/issues/14974))
7689
- Fixed issue with `DefaultAzureCredential` throwing exceptions during concurrent calls (Issue [#15013](https://github.com/Azure/azure-sdk-for-net/issues/15013))
7790

7891
## 1.2.2 (2020-08-20)
7992

8093
### Fixes and improvements
94+
8195
- Fixed issue with `InteractiveBrowserCredential` not specifying correct redirectUrl (Issue [#13940](https://github.com/Azure/azure-sdk-for-net/issues/13940))
8296

8397
## 1.2.1 (2020-08-18)
8498

8599
### Fixes and improvements
100+
86101
- Bug in TaskExtensions.EnsureCompleted method that causes it to unconditionally throw an exception in the environments with synchronization context
87102

88103
## 1.2.0 (2020-08-10)
89104

90105
### Breaking Changes
106+
91107
- Removing Application Authentication APIs for GA release. These will be reintroduced in 1.3.0-preview.
92108
- Removed class `AuthenticationRecord`
93109
- Removed class `AuthenticationRequiredException`
@@ -102,19 +118,21 @@
102118
- Removed properties `AllowUnencryptedCache`and `AuthenticationRecord` from `SharedTokenCacheCredentialOptions`
103119

104120
### Fixes and improvements
121+
105122
- Fixed excess errors in `DefaultAzureCredential` tracing (Issue [#10659](https://github.com/Azure/azure-sdk-for-net/issues/10659))
106123
- Fixed concurrency issue in `DefaultAzureCredential` (Issue [#13044](https://github.com/Azure/azure-sdk-for-net/issues/13044))
107124

108-
109125
## 1.2.0-preview.6 (2020-07-22)
110126

111127
### New Features
128+
112129
- Added the read only property `ClientId` to `AuthenticationRecord`.
113130
- Added the property `AllowUnencryptedCache` to the option classes `ClientCertificateCredentialOptions`, `ClientSecretCredentialOptions`, `DeviceCodeCredentialOptions`, `InteractiveBrowserCredentialOptions` and `SharedTokenCacheCredentialOptions` which when set to true allows the credential to fall back to storing tokens in an unencrypted file if no OS level user encryption is available when `EnablePersistentCache` is set to true.
114131
- Added the property `AuthenticationRecord` to the option class `SharedTokenCacheCredentialOptions` to support silent authentication for accounts previously authenticated with an interactive credential.
115132
- Added option class `UsernamePasswordCredentialOptions` which supports the options `EnablePersistentCache` and `AllowUnencryptedCache`.
116133

117134
### Breaking Changes
135+
118136
- Rename type `KnownAuthorityHosts` to `AzureAuthorityHosts`
119137
- Rename property `AzureChinaCloud` to `AzureChina`
120138
- Rename property `AzureGermanCloud` to `AzureGermany`
@@ -124,23 +142,26 @@
124142
## 1.2.0-preview.5 (2020-07-08)
125143

126144
### New Features
145+
127146
- Added options classes `ClientCertificateCredentialOptions` and `ClientSecretCredentialOptions` which support the following new option
128147
- `EnablePersistentCache` configures these credentials to use a persistent cache shared between credentials which set this option. By default the cache is per credential and in memory only.
129148

130-
131149
## 1.2.0-preview.4 (2020-06-10)
132150

133151
### New Features
152+
134153
- Makes `AzureCliCredential`, `VisualStudioCredential` and `VisualStudioCodeCredential` public to allow direct usage.
135154
- Added `Authenticate` methods to `UsernamePasswordCredential`
136155

137156
### Fixes and improvements
157+
138158
- Fix `SharedTokenCacheCredential` account filter to be case-insensitive (Issue [#10816](https://github.com/Azure/azure-sdk-for-net/issues/10816))
139159
- Update `VisualStudioCodeCredential` to properly throw `CredentialUnavailableException` when re-authentication is needed. (Issue [#11595](https://github.com/Azure/azure-sdk-for-net/issues/11595))
140160

141161
## 1.2.0-preview.3 (2020-05-05)
142162

143163
### New Features
164+
144165
- First preview of new API for authenticating users with `DeviceCodeCredential` and `InteractiveBrowserCredential`
145166
- Added method `Authenticate` which pro-actively interacts with the user to authenticate if necessary and returns a serializable `AuthenticationRecord`
146167
- Added Options classes `DeviceCodeCredentialOptions` and `InteractiveBrowserCredentialOptions` which support the following new options
@@ -151,23 +172,27 @@
151172
## 1.2.0-preview.2 (2020-04-06)
152173

153174
### New Features
175+
154176
- Updates `DefaultAzureCredential` to enable authenticating through Visual Studio
155177
- Updates `DefaultAzureCredential` to enable authentication through Visual Studio Code
156178

157179
## 1.2.0-preview.1 (2020-03-10)
158180

159181
### New Features
182+
160183
- Updating `DefaultAzureCredential` to enable authenticating through the Azure CLI
161184
- `ClientCertificateCredential` now supports being constructed with a path to an unencrypted certificate (in either PFX or PEM format)
162185
- `EnvironmentCredential` now supports reading a certificate path from `AZURE_CLIENT_CERTIFICATE_PATH`
163186

164187
### Fixes and improvements
188+
165189
- Fix an issue where `EnvironmentCredential` did not behave correctly when `AZURE_USERNAME` and `AZURE_PASSWORD` where set
166190
- Added `KnownAuthorityHosts` class to aid in sovereign cloud configuration.
167191

168192
## 1.1.1 (2020-02-10)
169193

170194
### Fixes and improvements
195+
171196
- Fixed `UsernamePasswordCredential` constructor parameter mishandling
172197
- Updated `ManagedIdentityCredential` endpoint discovery to avoid throwing
173198
- Fixed `ManagedIdentityCredential` to raise `CredentialUnavailableException` on 400 return from the service where no identity has been assigned
@@ -176,6 +201,7 @@
176201
## 1.1.0 (2019-11-25)
177202

178203
### Fixes and improvements
204+
179205
- Update `SharedTokenCacheCredential` to filter accounts by tenant id
180206
- Added `SharedTokenCacheCredentialOptions` class with properties `TenantId` and `Username`
181207
- Added constructor overload to `SharedTokenCacheCredential` which accepts `SharedTokenCacheCredentialOptions`
@@ -185,9 +211,11 @@
185211
- Fixed issue with `ManagedIdentityCredential` authentication with user assigned identities
186212

187213
## 1.0.0 (2019-10-29)
214+
188215
- First stable release of Azure.Identity package.
189216

190217
### Breaking Changes
218+
191219
- Rename `AzureCredentialOptions` -> `TokenCredentialOptions`
192220
- Renamed property `VerificationUrl` -> `VerificationUri` and changed type from `string` to `Uri`
193221
- Updated `ClientSecretCredential` class
@@ -219,66 +247,76 @@
219247
- Added the `CredentialUnavailableExcpetion` exception type to distinguish cases when failure to obtain an `AccessToken` was expected
220248

221249
### Dependency Changes
250+
222251
- Adopted Azure.Core 1.0.0
223252

224253
### Fixes and improvements
254+
225255
- Update `ManagedIdentityCredential` IMDS availability check to handle immediate network failures
226256
- Added a `DefaultAzureCredential` constructor overload to enable interactive credential types by default
227257

228258
## 1.0.0-preview.5 (2019-10-07)
229259

230260
### Dependency Changes
261+
231262
- Adopted Azure.Core 1.0.0-preview.9
232263

233264
### New Features
265+
234266
- Added `DefaultAzureCredentialOptions` for configuring the `DefaultAzureCredential` authentication flow
235267
- Added `InteractiveBrowserCredential` to the `DefaultAzureCredential` authentication flow, but excluded by default
236268

237269
### Fixes and improvements
270+
238271
- Updated `InteractiveBrowserCredential` and `DeviceCodeCredential` to optionally accept a tenantId to support non-multitenant applications
239272

240273
## 1.0.0-preview.4 (2019-09-10)
241274

242275
### Breaking Changes
276+
243277
- Modified GetToken abstraction to accept `TokenRequest` structure rather than `string[]` for forwards compatibility
244278

245279
### Dependency Changes
280+
246281
- Adopted Azure.Core 1.0.0-preview.8
247282

248283
### New Features
284+
249285
- Added `SharedTokenCacheCredential` to support Single Sign On with developer tooling
250286
- Updated `DefaultAzureCredential`authentication flow to include the `SharedTokenCacheCredential`
251287

252-
253288
## 1.0.0-preview.3 (2019-08-06)
254289

255290
### Dependency Changes
291+
256292
- Adopted Azure.Core 1.0.0-preview.7
257293
- Adopted Microsoft.Identity.Client 4.1.0
258294

259295
### New Features
296+
260297
- User Principal Authentication
261298
- Added `DeviceCodeCredential` class
262299
- Added `InteractiveBrowserCredential` class
263300
- Added `UsernamePasswordCredential` class
264301
- Support for Azure SDK ASP .NET Core integration
265302

266303
### Fixes and improvements
267-
- Added identity client distributed tracing support
268304

305+
- Added identity client distributed tracing support
269306

270307
## 1.0.0-preview.2 (2019-07-02)
271308

272309
### Fixes and improvements
273-
- Fix to ManagedIdentityCredential to properly parse expires_on from response
274310

311+
- Fix to ManagedIdentityCredential to properly parse expires_on from response
275312

276313
## 1.0.0-preview.1 (2019-06-27)
277314

278315
Version 1.0.0-preview.1 is the first preview of our efforts to create a user-friendly authentication API for Azure SDK client libraries. For more
279316
information about preview releases of other Azure SDK libraries, please visit https://aka.ms/azure-sdk-preview1-net.
280317

281318
### New Features
319+
282320
- Azure Service Authentication
283321
- Added `DefaultAzureCredential` class
284322
- Added `ChainedTokenCredential` class

sdk/identity/Azure.Identity/api/Azure.Identity.netstandard2.0.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,12 @@ public SharedTokenCacheCredentialOptions(Azure.Identity.TokenCache tokenCache) {
225225
public Azure.Identity.TokenCache TokenCache { get { throw null; } }
226226
public string Username { get { throw null; } set { } }
227227
}
228-
public partial class TokenCache : System.IDisposable
228+
public partial class TokenCache
229229
{
230230
public TokenCache() { }
231231
public event System.Func<Azure.Identity.TokenCacheUpdatedArgs, System.Threading.Tasks.Task> Updated { add { } remove { } }
232232
public static Azure.Identity.TokenCache Deserialize(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
233233
public static System.Threading.Tasks.Task<Azure.Identity.TokenCache> DeserializeAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
234-
public void Dispose() { }
235-
protected virtual void Dispose(bool disposing) { }
236234
public void Serialize(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { }
237235
public System.Threading.Tasks.Task SerializeAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
238236
}

sdk/identity/Azure.Identity/src/TokenCache.cs

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22
// Licensed under the MIT License.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.IO;
7-
using System.Linq;
86
using System.Runtime.CompilerServices;
97
using System.Threading;
108
using System.Threading.Tasks;
119
using Azure.Core.Pipeline;
1210
using Microsoft.Identity.Client;
13-
using Microsoft.Identity.Client.Extensions.Msal;
1411

1512
namespace Azure.Identity
1613
{
1714
/// <summary>
1815
/// A cache for Tokens.
1916
/// </summary>
20-
public class TokenCache : IDisposable
17+
#pragma warning disable CA1001 // Types that own disposable fields should be disposable
18+
// SemaphoreSlim only needs to be disposed when AvailableWaitHandle is called.
19+
public class TokenCache
20+
#pragma warning restore CA1001 // Types that own disposable fields should be disposable
2121
{
2222
private SemaphoreSlim _lock = new SemaphoreSlim(1,1);
2323
private byte[] _data;
2424
private DateTimeOffset _lastUpdated;
2525
private ConditionalWeakTable<object, CacheTimestamp> _cacheAccessMap;
26-
private bool _disposedValue;
2726

2827
private class CacheTimestamp
2928
{
@@ -172,11 +171,6 @@ internal virtual async Task RegisterCache(bool async, ITokenCache tokenCache, Ca
172171

173172
private async Task OnBeforeCacheAccessAsync(TokenCacheNotificationArgs args)
174173
{
175-
if (_disposedValue)
176-
{
177-
throw new ObjectDisposedException(nameof(TokenCache));
178-
}
179-
180174
await _lock.WaitAsync().ConfigureAwait(false);
181175

182176
try
@@ -193,11 +187,6 @@ private async Task OnBeforeCacheAccessAsync(TokenCacheNotificationArgs args)
193187

194188
private async Task OnAfterCacheAccessAsync(TokenCacheNotificationArgs args)
195189
{
196-
if (_disposedValue)
197-
{
198-
throw new ObjectDisposedException(nameof(TokenCache));
199-
}
200-
201190
if (args.HasStateChanged)
202191
{
203192
await UpdateCacheDataAsync(args.TokenCache).ConfigureAwait(false);
@@ -255,36 +244,5 @@ private static async Task<byte[]> MergeCacheData(byte[] cacheA, byte[] cacheB)
255244

256245
return merged;
257246
}
258-
259-
/// <summary>
260-
/// Disposes of the <see cref="TokenCache"/>.
261-
/// </summary>
262-
/// <param name="disposing">Indicates whether managed resources should be disposed.</param>
263-
protected virtual void Dispose(bool disposing)
264-
{
265-
if (!_disposedValue)
266-
{
267-
if (disposing)
268-
{
269-
_lock.Dispose();
270-
}
271-
272-
_cacheAccessMap = null;
273-
274-
_data = null;
275-
276-
_disposedValue = true;
277-
}
278-
}
279-
280-
/// <summary>
281-
/// Disposes of the <see cref="TokenCache"/>.
282-
/// </summary>
283-
public void Dispose()
284-
{
285-
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
286-
Dispose(disposing: true);
287-
GC.SuppressFinalize(this);
288-
}
289247
}
290248
}

0 commit comments

Comments
 (0)