Skip to content

Commit e3a059f

Browse files
christothesbgavrilMSjennyf19
authored
Use ManagedIdentityCredential instead of DefaultAzureCredential for FIC scenario (PR for ID.Web 3.x) (#2813)
* Use ManagedIdentityCredential instead of DefaultAzureCredential for MI scenarios * Address PR comments * Drop Azure.Identity and use just MSAL here. * PR comment * Fix --------- Co-authored-by: Bogdan Gavril <[email protected]> Co-authored-by: jennyf19 <[email protected]>
1 parent 3693193 commit e3a059f

File tree

3 files changed

+21
-25
lines changed

3 files changed

+21
-25
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
<MicrosoftExtensionsConfigurationBinderVersion>9.0.100-preview.4.24267.66</MicrosoftExtensionsConfigurationBinderVersion>
106106
<MicrosoftExtensionsDependencyInjectionVersion>$(NetNineRuntimeVersion)</MicrosoftExtensionsDependencyInjectionVersion>
107107
</PropertyGroup>
108-
108+
109109
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
110110
<MicrosoftAspNetCoreAuthenticationJwtBearerVersion>8.0.0</MicrosoftAspNetCoreAuthenticationJwtBearerVersion>
111111
<MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>8.0.0</MicrosoftAspNetCoreAuthenticationOpenIdConnectVersion>

src/Microsoft.Identity.Web.Certificateless/ManagedIdentityClientAssertion.cs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33

44
using System.Threading;
55
using System.Threading.Tasks;
6-
using Azure.Core;
7-
using Azure.Identity;
86
using Microsoft.Identity.Client;
7+
using Microsoft.Identity.Client.AppConfig;
98
using Microsoft.Identity.Web.Certificateless;
109

1110
namespace Microsoft.Identity.Web
@@ -15,36 +14,31 @@ namespace Microsoft.Identity.Web
1514
/// </summary>
1615
public class ManagedIdentityClientAssertion : ClientAssertionProviderBase
1716
{
18-
private readonly TokenCredential _credential;
17+
IManagedIdentityApplication _managedIdentityApplication;
1918
private readonly string _tokenExchangeUrl;
2019

2120
/// <summary>
2221
/// See https://aka.ms/ms-id-web/certificateless.
2322
/// </summary>
24-
/// <param name="managedIdentityClientId">Optional ClientId of the Managed Identity or Workload Identity</param>
23+
/// <param name="managedIdentityClientId">Optional ClientId of the Managed Identity</param>
2524
public ManagedIdentityClientAssertion(string? managedIdentityClientId)
2625
{
27-
_credential = new DefaultAzureCredential(
28-
new DefaultAzureCredentialOptions
29-
{
30-
ManagedIdentityClientId = managedIdentityClientId,
31-
WorkloadIdentityClientId = managedIdentityClientId,
32-
ExcludeAzureCliCredential = true,
33-
ExcludeAzureDeveloperCliCredential = true,
34-
ExcludeAzurePowerShellCredential = true,
35-
ExcludeInteractiveBrowserCredential = true,
36-
ExcludeSharedTokenCacheCredential = true,
37-
ExcludeVisualStudioCodeCredential = true,
38-
ExcludeVisualStudioCredential = true
39-
});
26+
var id = ManagedIdentityId.SystemAssigned;
27+
if (!string.IsNullOrEmpty(managedIdentityClientId))
28+
{
29+
id = ManagedIdentityId.WithUserAssignedClientId(managedIdentityClientId);
30+
}
31+
32+
_managedIdentityApplication = ManagedIdentityApplicationBuilder.Create(id).Build();
4033
_tokenExchangeUrl = CertificatelessConstants.DefaultTokenExchangeUrl;
4134
}
4235

4336
/// <summary>
4437
/// See https://aka.ms/ms-id-web/certificateless.
4538
/// </summary>
46-
/// <param name="managedIdentityClientId">Optional ClientId of the Managed Identity or Workload Identity</param>
47-
/// <param name="tokenExchangeUrl">Optional token exchange resource url. Default value is "api://AzureADTokenExchange/.default".</param>
39+
/// <param name="managedIdentityClientId">Optional ClientId of the Managed Identity</param>
40+
/// <param name="tokenExchangeUrl">Optional audience of the token to be requested from Managed Identity. Default value is "api://AzureADTokenExchange".
41+
/// This value is different on clouds other than Azure Public</param>
4842
public ManagedIdentityClientAssertion(string? managedIdentityClientId, string? tokenExchangeUrl) : this (managedIdentityClientId)
4943
{
5044
_tokenExchangeUrl = tokenExchangeUrl ?? CertificatelessConstants.DefaultTokenExchangeUrl;
@@ -57,10 +51,12 @@ public ManagedIdentityClientAssertion(string? managedIdentityClientId, string? t
5751
/// <returns>The signed assertion.</returns>
5852
protected override async Task<ClientAssertion> GetClientAssertionAsync(AssertionRequestOptions? assertionRequestOptions)
5953
{
60-
var result = await _credential.GetTokenAsync(
61-
new TokenRequestContext([_tokenExchangeUrl], null),
62-
assertionRequestOptions?.CancellationToken ?? default).ConfigureAwait(false);
63-
return new ClientAssertion(result.Token, result.ExpiresOn);
54+
var result = await _managedIdentityApplication
55+
.AcquireTokenForManagedIdentity(_tokenExchangeUrl)
56+
.ExecuteAsync(assertionRequestOptions?.CancellationToken ?? CancellationToken.None)
57+
.ConfigureAwait(false);
58+
59+
return new ClientAssertion(result.AccessToken, result.ExpiresOn);
6460
}
6561
}
6662
}

src/Microsoft.Identity.Web.Certificateless/Microsoft.Identity.Web.Certificateless.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Azure.Identity" Version="$(AzureIdentityVersion)" />
1514
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsLoggingVersion)" />
1615
<PackageReference Include="System.Text.Encodings.Web" Version="$(SystemTextEncodingsWebVersion)" />
1716
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens " Version="$(IdentityModelVersion)" />
17+
<PackageReference Include="Microsoft.Identity.Client" Version="$(MicrosoftIdentityClientVersion)" />
1818
</ItemGroup>
1919
</Project>

0 commit comments

Comments
 (0)