Skip to content

Commit 5744f7c

Browse files
committed
Fixing login issue when final tenant ID contains no subscriptions
1 parent 765b5ab commit 5744f7c

File tree

3 files changed

+51
-35
lines changed

3 files changed

+51
-35
lines changed

src/Common/Commands.ResourceManager.Common/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common/Commands.ResourceManager.Common/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ Select Y to enable data collection [Y/N]:</value>
148148
<data name="InvalidDefaultSubscription" xml:space="preserve">
149149
<value>No default subscription has been designated. Use Select-AzureSubscription -Default &lt;subscriptionName&gt; to set the default subscription.</value>
150150
</data>
151+
<data name="NoSubscriptionFound" xml:space="preserve">
152+
<value>The provided account {0} does not have access to any subscriptions. Please try logging in with different credentials.</value>
153+
</data>
151154
<data name="TenantNotFound" xml:space="preserve">
152155
<value>Tenant '{0}' was not found. Please verify that your account has access to this tenant.</value>
153156
</data>

src/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
5454
// (tenant is present and subscription is not provided)
5555
if (!string.IsNullOrEmpty(tenantId))
5656
{
57-
TryGetTenantSubscription(account, environment, tenantId, subscriptionId, password, promptBehavior, out newSubscription, out newTenant);
57+
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
58+
TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, out newSubscription, out newTenant);
5859
}
5960
// (tenant is not provided and subscription is present) OR
6061
// (tenant is not provided and subscription is not provided)
@@ -63,16 +64,21 @@ public AzureRMProfile Login(AzureAccount account, AzureEnvironment environment,
6364
foreach(var tenant in ListAccountTenants(account, environment, password, promptBehavior))
6465
{
6566
AzureTenant tempTenant;
66-
if (TryGetTenantSubscription(account, environment, tenant.Id.ToString(), subscriptionId, password, ShowDialog.Auto, out newSubscription, out tempTenant))
67-
{
67+
AzureSubscription tempSubscription;
68+
var token = AcquireAccessToken(account, environment, tenant.Id.ToString(), password,
69+
ShowDialog.Auto);
70+
if (newTenant == null && TryGetTenantSubscription(token, account, environment, tenant.Id.ToString(), subscriptionId, out tempSubscription, out tempTenant) &&
71+
newTenant == null)
72+
{
6873
newTenant = tempTenant;
74+
newSubscription = tempSubscription;
6975
}
7076
}
7177
}
7278

7379
if (newSubscription == null)
7480
{
75-
throw new PSInvalidOperationException("Subscription was not found.");
81+
throw new PSInvalidOperationException(String.Format(Properties.Resources.NoSubscriptionFound, account.Id));
7682
}
7783

7884
_profile.Context = new AzureContext(newSubscription, account, environment, newTenant);
@@ -135,12 +141,14 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
135141
{
136142
if (string.IsNullOrWhiteSpace(tenantId))
137143
{
138-
throw new ArgumentNullException("Please provide a valid tenant Id");
144+
throw new ArgumentNullException("Please provide a valid tenant Id.");
139145
}
140146

141147
AzureTenant tenant;
142-
return TryGetTenantSubscription(_profile.Context.Account, _profile.Context.Environment,
143-
tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant);
148+
var token = AcquireAccessToken(_profile.Context.Account, _profile.Context.Environment,
149+
tenantId, null, ShowDialog.Never);
150+
return TryGetTenantSubscription(token, _profile.Context.Account, _profile.Context.Environment,
151+
tenantId, subscriptionId, out subscription, out tenant);
144152
}
145153

146154
public AzureEnvironment AddOrSetEnvironment(AzureEnvironment environment)
@@ -235,23 +243,30 @@ private AzureEnvironment MergeEnvironmentProperties(AzureEnvironment environment
235243
return mergedEnvironment;
236244
}
237245

238-
private bool TryGetTenantSubscription(
239-
AzureAccount account,
240-
AzureEnvironment environment,
241-
string tenantId,
242-
string subscriptionId,
243-
SecureString password,
244-
ShowDialog promptBehavior,
246+
private IAccessToken AcquireAccessToken(AzureAccount account,
247+
AzureEnvironment environment,
248+
string tenantId,
249+
SecureString password,
250+
ShowDialog promptBehavior)
251+
{
252+
return AzureSession.AuthenticationFactory.Authenticate(
253+
account,
254+
environment,
255+
tenantId,
256+
password,
257+
promptBehavior,
258+
TokenCache.DefaultShared);
259+
}
260+
261+
private bool TryGetTenantSubscription(IAccessToken accessToken,
262+
AzureAccount account,
263+
AzureEnvironment environment,
264+
string tenantId,
265+
string subscriptionId,
245266
out AzureSubscription subscription,
246267
out AzureTenant tenant)
247268
{
248-
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
249-
account,
250-
environment,
251-
tenantId,
252-
password,
253-
promptBehavior,
254-
TokenCache.DefaultShared);
269+
255270
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
256271
new TokenCloudCredentials(accessToken.AccessToken),
257272
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))
@@ -311,13 +326,8 @@ private bool TryGetTenantSubscription(
311326

312327
private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
313328
{
314-
var commonTenantToken = AzureSession.AuthenticationFactory.Authenticate(
315-
account,
316-
environment,
317-
AuthenticationFactory.CommonAdTenant,
318-
password,
319-
promptBehavior,
320-
TokenCache.DefaultShared);
329+
var commonTenantToken = AcquireAccessToken( account, environment, AuthenticationFactory.CommonAdTenant,
330+
password, promptBehavior);
321331

322332
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
323333
new TokenCloudCredentials(commonTenantToken.AccessToken),
@@ -341,13 +351,7 @@ public IEnumerable<AzureTenant> ListTenants()
341351
private IEnumerable<AzureSubscription> ListSubscriptionsForTenant(AzureAccount account, AzureEnvironment environment,
342352
SecureString password, ShowDialog promptBehavior, string tenantId)
343353
{
344-
var accessToken = AzureSession.AuthenticationFactory.Authenticate(
345-
account,
346-
environment,
347-
tenantId,
348-
password,
349-
promptBehavior,
350-
TokenCache.DefaultShared);
354+
var accessToken = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
351355
using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>(
352356
new TokenCloudCredentials(accessToken.AccessToken),
353357
environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager)))

0 commit comments

Comments
 (0)