@@ -34,7 +34,10 @@ namespace Microsoft.Azure.Commands.Common.Authentication
34
34
public abstract class PowerShellTokenCacheProvider
35
35
{
36
36
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" ;
38
41
39
42
protected byte [ ] _tokenCacheDataToFlush ;
40
43
@@ -98,7 +101,7 @@ public List<IAccessToken> GetTenantTokensForAccount(IAccount account, IAzureEnvi
98
101
Id = account . Username ,
99
102
Type = AzureAccount . AccountType . User
100
103
} ;
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 ) ;
102
105
IEnumerable < string > tenants = Enumerable . Empty < string > ( ) ;
103
106
using ( SubscriptionClient subscriptionClient = GetSubscriptionClient ( commonToken , environment ) )
104
107
{
@@ -164,6 +167,25 @@ private SubscriptionClient GetSubscriptionClient(IAccessToken token, IAzureEnvir
164
167
165
168
protected abstract void RegisterCache ( IPublicClientApplication client ) ;
166
169
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
+ }
167
189
/// <summary>
168
190
/// Creates a public client app.
169
191
/// This method is not meant for authentication purpose. Use APIs from Azure.Identity instead.
0 commit comments