Skip to content

Commit 381cde6

Browse files
CopilotJonathanCrdscottaddie
authored
[Identity] Deprecate legacy ManagedIdentityCredential constructors (#53825)
* Initial plan * Add Obsolete attributes to legacy ManagedIdentityCredential constructors and update usages Co-authored-by: JonathanCrd <[email protected]> * Fix Obsolete attribute message to use correct constructor signature Co-authored-by: JonathanCrd <[email protected]> * Update AzureClientFactoryTests to use new ManagedIdentityCredential constructor Co-authored-by: scottaddie <[email protected]> * Update API listing files after adding Obsolete attributes Co-authored-by: scottaddie <[email protected]> * Update ManagedIdentityTests to use new constructor pattern Co-authored-by: JonathanCrd <[email protected]> * Add issue reference to CHANGELOG entry Co-authored-by: scottaddie <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: JonathanCrd <[email protected]> Co-authored-by: scottaddie <[email protected]>
1 parent 37d9f92 commit 381cde6

File tree

7 files changed

+20
-7
lines changed

7 files changed

+20
-7
lines changed

sdk/extensions/Microsoft.Extensions.Azure/src/Internal/ClientFactory.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ internal static TokenCredential CreateCredential(IConfiguration configuration)
126126

127127
if (!string.IsNullOrWhiteSpace(resourceId))
128128
{
129-
return new ManagedIdentityCredential(new ResourceIdentifier(resourceId));
129+
return new ManagedIdentityCredential(ManagedIdentityId.FromUserAssignedResourceId(new ResourceIdentifier(resourceId)));
130130
}
131131

132132
if (!string.IsNullOrWhiteSpace(objectId))
@@ -136,10 +136,15 @@ internal static TokenCredential CreateCredential(IConfiguration configuration)
136136

137137
if (!string.IsNullOrWhiteSpace(managedIdentityClientId))
138138
{
139-
return new ManagedIdentityCredential(managedIdentityClientId);
139+
return new ManagedIdentityCredential(ManagedIdentityId.FromUserAssignedClientId(managedIdentityClientId));
140140
}
141141

142-
return new ManagedIdentityCredential(clientId);
142+
if (!string.IsNullOrWhiteSpace(clientId))
143+
{
144+
return new ManagedIdentityCredential(ManagedIdentityId.FromUserAssignedClientId(clientId));
145+
}
146+
147+
return new ManagedIdentityCredential(ManagedIdentityId.SystemAssigned);
143148
}
144149

145150
if (string.Equals(credentialType, "workloadidentity", StringComparison.OrdinalIgnoreCase))

sdk/extensions/Microsoft.Extensions.Azure/tests/AzureClientFactoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public void ResolvesDefaultClientByDefault()
238238
public void UsesProvidedCredentialIfOverGlobal()
239239
{
240240
var serviceCollection = new ServiceCollection();
241-
var defaultAzureCredential = new ManagedIdentityCredential();
241+
var defaultAzureCredential = new ManagedIdentityCredential(ManagedIdentityId.SystemAssigned);
242242
serviceCollection.AddAzureClients(builder => builder.AddTestClientWithCredentials(new Uri("http://localhost")).WithCredential(defaultAzureCredential));
243243

244244
ServiceProvider provider = serviceCollection.BuildServiceProvider();
@@ -251,7 +251,7 @@ public void UsesProvidedCredentialIfOverGlobal()
251251
public void UsesGlobalCredential()
252252
{
253253
var serviceCollection = new ServiceCollection();
254-
var defaultAzureCredential = new ManagedIdentityCredential();
254+
var defaultAzureCredential = new ManagedIdentityCredential(ManagedIdentityId.SystemAssigned);
255255
serviceCollection.AddAzureClients(builder => {
256256
builder.AddTestClientWithCredentials(new Uri("http://localhost"));
257257
builder.UseCredential(defaultAzureCredential);

sdk/identity/Azure.Identity/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
### Other Changes
1212

13+
- Deprecated legacy `ManagedIdentityCredential` constructors. Use `ManagedIdentityCredential(ManagedIdentityId id)` or `ManagedIdentityCredential(ManagedIdentityCredentialOptions options)` instead for clearer intent when specifying system-assigned or user-assigned managed identity. ([#53800](https://github.com/Azure/azure-sdk-for-net/issues/53800))
14+
1315
## 1.17.0 (2025-10-07)
1416

1517
### Bugs Fixed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,12 @@ public partial class ManagedIdentityCredential : Azure.Core.TokenCredential
325325
{
326326
protected ManagedIdentityCredential() { }
327327
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
328+
[System.ObsoleteAttribute("Use constructor ManagedIdentityCredential(ManagedIdentityId id) or ManagedIdentityCredential(ManagedIdentityCredentialOptions options).")]
328329
public ManagedIdentityCredential(Azure.Core.ResourceIdentifier resourceId, Azure.Identity.TokenCredentialOptions options = null) { }
329330
public ManagedIdentityCredential(Azure.Identity.ManagedIdentityCredentialOptions options) { }
330331
public ManagedIdentityCredential(Azure.Identity.ManagedIdentityId id) { }
331332
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
333+
[System.ObsoleteAttribute("Use constructor ManagedIdentityCredential(ManagedIdentityId id) or ManagedIdentityCredential(ManagedIdentityCredentialOptions options).")]
332334
public ManagedIdentityCredential(string clientId = null, Azure.Identity.TokenCredentialOptions options = null) { }
333335
public override Azure.Core.AccessToken GetToken(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
334336
public override System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,12 @@ public partial class ManagedIdentityCredential : Azure.Core.TokenCredential
322322
{
323323
protected ManagedIdentityCredential() { }
324324
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
325+
[System.ObsoleteAttribute("Use constructor ManagedIdentityCredential(ManagedIdentityId id) or ManagedIdentityCredential(ManagedIdentityCredentialOptions options).")]
325326
public ManagedIdentityCredential(Azure.Core.ResourceIdentifier resourceId, Azure.Identity.TokenCredentialOptions options = null) { }
326327
public ManagedIdentityCredential(Azure.Identity.ManagedIdentityCredentialOptions options) { }
327328
public ManagedIdentityCredential(Azure.Identity.ManagedIdentityId id) { }
328329
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
330+
[System.ObsoleteAttribute("Use constructor ManagedIdentityCredential(ManagedIdentityId id) or ManagedIdentityCredential(ManagedIdentityCredentialOptions options).")]
329331
public ManagedIdentityCredential(string clientId = null, Azure.Identity.TokenCredentialOptions options = null) { }
330332
public override Azure.Core.AccessToken GetToken(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
331333
public override System.Threading.Tasks.ValueTask<Azure.Core.AccessToken> GetTokenAsync(Azure.Core.TokenRequestContext requestContext, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

sdk/identity/Azure.Identity/integration/Integration.Identity.Common/ManagedIdentityTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public static void AuthToStorage()
2121
string account1 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_1")!;
2222
string account2 = Environment.GetEnvironmentVariable("IDENTITY_STORAGE_NAME_2")!;
2323

24-
var credential1 = new ManagedIdentityCredential();
25-
var credential2 = new ManagedIdentityCredential(new ResourceIdentifier(resourceId));
24+
var credential1 = new ManagedIdentityCredential(ManagedIdentityId.SystemAssigned);
25+
var credential2 = new ManagedIdentityCredential(ManagedIdentityId.FromUserAssignedResourceId(new ResourceIdentifier(resourceId)));
2626
var client1 = new BlobServiceClient(new Uri($"https://{account1}.blob.core.windows.net/"), credential1);
2727
var client2 = new BlobServiceClient(new Uri($"https://{account2}.blob.core.windows.net/"), credential2);
2828
client1.GetBlobContainers().ToList();

sdk/identity/Azure.Identity/src/Credentials/ManagedIdentityCredential.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected ManagedIdentityCredential()
4444
/// </param>
4545
/// <param name="options">Options to configure the management of the requests sent to Microsoft Entra ID.</param>
4646
[EditorBrowsable(EditorBrowsableState.Never)]
47+
[Obsolete("Use constructor ManagedIdentityCredential(ManagedIdentityId id) or ManagedIdentityCredential(ManagedIdentityCredentialOptions options).")]
4748
public ManagedIdentityCredential(string clientId = null, TokenCredentialOptions options = null)
4849
: this(new ManagedIdentityClient(new ManagedIdentityClientOptions { ManagedIdentityId = string.IsNullOrEmpty(clientId) ? ManagedIdentityId.SystemAssigned : ManagedIdentityId.FromUserAssignedClientId(clientId), Pipeline = CredentialPipeline.GetInstance(options, IsManagedIdentityCredential: true), Options = options }))
4950
{
@@ -58,6 +59,7 @@ public ManagedIdentityCredential(string clientId = null, TokenCredentialOptions
5859
/// </param>
5960
/// <param name="options">Options to configure the management of the requests sent to Microsoft Entra ID.</param>
6061
[EditorBrowsable(EditorBrowsableState.Never)]
62+
[Obsolete("Use constructor ManagedIdentityCredential(ManagedIdentityId id) or ManagedIdentityCredential(ManagedIdentityCredentialOptions options).")]
6163
public ManagedIdentityCredential(ResourceIdentifier resourceId, TokenCredentialOptions options = null)
6264
: this(new ManagedIdentityClient(new ManagedIdentityClientOptions { ManagedIdentityId = ManagedIdentityId.FromUserAssignedResourceId(resourceId), Pipeline = CredentialPipeline.GetInstance(options, IsManagedIdentityCredential: true), Options = options }))
6365
{

0 commit comments

Comments
 (0)