Skip to content

Commit d0358fe

Browse files
authored
Pass Microsoft EntraID Authority to CreatePublicClient to Fix Az.Ssh Issue When WAM Enabled (#25944)
* Address review comments Address review comments Polish change log Address review comments Address review comments * Integrate Microsoft.Identity.Client 4.65.0 * Polish change log
1 parent ed311cd commit d0358fe

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Fixed the issue that `Export-AzSshConfig` and `Enter-AzVM` from Az.Ssh are not able to use when WAM is enabled.
2223
* Added breaking change preannouncement for the removal of alias `Resolve-Error`. #26189
2324
* Integrated new detection library to expand the scope of secrets.
2425
* Upgraded Azure.Core to 1.44.1.

src/Accounts/AssemblyLoading/ConditionalAssemblyProvider.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ public static void Initialize(string rootPath, IConditionalAssemblyContext conte
4646
CreateAssembly("netstandard2.0", "Azure.Identity", "1.12.0.0"),
4747
CreateAssembly("netstandard2.0", "Azure.Identity.Broker", "1.1.0.0"),
4848
CreateAssembly("netstandard2.0", "Microsoft.Bcl.AsyncInterfaces", "6.0.0.0"),
49-
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client", "4.61.3.0"),
50-
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Extensions.Msal", "4.61.3.0"),
51-
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Broker", "4.61.3.0"),
49+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client", "4.65.0.0"),
50+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Extensions.Msal", "4.65.0.0"),
51+
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.Broker", "4.65.0.0"),
5252
CreateAssembly("netstandard2.0", "Microsoft.Identity.Client.NativeInterop", "0.16.2.0"),
5353
CreateAssembly("netstandard2.0", "Microsoft.IdentityModel.Abstractions", "6.35.0.0"),
5454
CreateAssembly("netstandard2.0", "System.ClientModel", "1.1.0.0"),

src/Accounts/Authentication/Authentication/TokenCache/PowerShellTokenCacheProvider.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ namespace Microsoft.Azure.Commands.Common.Authentication
3434
public abstract class PowerShellTokenCacheProvider
3535
{
3636
public const string PowerShellTokenCacheProviderKey = "PowerShellTokenCacheProviderKey";
37-
private static readonly string CommonTenant = "organizations";
37+
//Reanme CommonTenant to OrganizationTenant with reference to
38+
//https://learn.microsoft.com/en-us/dotnet/api/microsoft.identity.client.abstractapplicationbuilder-1.withauthority?view=msal-dotnet-latest#microsoft-identity-client-abstractapplicationbuilder-1-withauthority(system-string-system-boolean
39+
//From MSAL, we shall always use "organizations" for both work and school and MSA accounts
40+
private const string organizationTenant = "organizations";
3841

3942
protected byte[] _tokenCacheDataToFlush;
4043

@@ -98,7 +101,7 @@ public List<IAccessToken> GetTenantTokensForAccount(IAccount account, IAzureEnvi
98101
Id = account.Username,
99102
Type = AzureAccount.AccountType.User
100103
};
101-
var commonToken = AzureSession.Instance.AuthenticationFactory.Authenticate(azureAccount, environment, CommonTenant, null, null, promptAction);
104+
var commonToken = AzureSession.Instance.AuthenticationFactory.Authenticate(azureAccount, environment, organizationTenant, null, null, promptAction);
102105
IEnumerable<string> tenants = Enumerable.Empty<string>();
103106
using (SubscriptionClient subscriptionClient = GetSubscriptionClient(commonToken, environment))
104107
{
@@ -164,6 +167,25 @@ private SubscriptionClient GetSubscriptionClient(IAccessToken token, IAzureEnvir
164167

165168
protected abstract void RegisterCache(IPublicClientApplication client);
166169

170+
/// <summary>
171+
/// Creates a public client app with tenantId.
172+
/// This method is not meant for authentication purpose. Use APIs from Azure.Identity instead.
173+
/// </summary>
174+
public virtual IPublicClientApplication CreatePublicClient(string authority, string tenantId)
175+
{
176+
var builder = PublicClientApplicationBuilder.Create(Constants.PowerShellClientId);
177+
if (AzConfigReader.IsWamEnabled(authority))
178+
{
179+
builder = builder.WithBroker(new BrokerOptions(BrokerOptions.OperatingSystems.Windows));
180+
}
181+
if (!string.IsNullOrEmpty(authority))
182+
{
183+
builder.WithAuthority(authority, tenantId ?? organizationTenant);
184+
}
185+
var client = builder.Build();
186+
RegisterCache(client);
187+
return client;
188+
}
167189
/// <summary>
168190
/// Creates a public client app.
169191
/// This method is not meant for authentication purpose. Use APIs from Azure.Identity instead.

src/Accounts/Authentication/Factories/SshCredentialFactory.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
using Microsoft.Azure.Commands.Common.Authentication.Abstractions;
1616
using Microsoft.Azure.Commands.Common.Authentication.Abstractions.Models;
17-
using Microsoft.Azure.Commands.Common.Authentication.Authentication;
1817
using Microsoft.Azure.Commands.Common.Authentication.Properties;
1918
using Microsoft.Identity.Client.SSHCertificates;
2019
using Microsoft.WindowsAzure.Commands.Utilities.Common;
@@ -69,7 +68,7 @@ public SshCredential GetSshCredential(IAzureContext context, RSAParameters rsaKe
6968
throw new NullReferenceException(Resources.AuthenticationClientFactoryNotRegistered);
7069
}
7170

72-
var publicClient = tokenCacheProvider.CreatePublicClient();
71+
var publicClient = tokenCacheProvider.CreatePublicClient(context.Environment.ActiveDirectoryAuthority, context.Tenant.Id);
7372
string cloudName = context.Environment.Name.ToLower();
7473
string scope = CloudToScope.GetValueOrDefault(cloudName, null);
7574
if (scope == null)
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)