Skip to content

Commit 7b3b86a

Browse files
authored
Merge pull request #6822 from cormacpayne/domain-tenant
Enable passing a tenant domain to Connect-AzureRmAccount
2 parents 972be0a + 4659e24 commit 7b3b86a

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

src/Common/Commands.Common.Authentication/Authentication/UserTokenProvider.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ private AuthenticationContext CreateContext(AdalConfiguration config)
121121
// We have to run this in a separate thread to guarantee that it's STA. This method
122122
// handles the threading details.
123123
private AuthenticationResult AcquireToken(
124-
AdalConfiguration config,
125-
string promptBehavior,
126-
Action<string> promptAction,
124+
AdalConfiguration config,
125+
string promptBehavior,
126+
Action<string> promptAction,
127127
string userId,
128128
SecureString password)
129129
{
@@ -239,6 +239,21 @@ private AuthenticationResult DoAcquireToken(
239239
AdalTokenCache.ClearCookies();
240240
}
241241

242+
Guid tempGuid = Guid.Empty;
243+
if (!string.Equals(config.AdDomain, "Common", StringComparison.OrdinalIgnoreCase) && !Guid.TryParse(config.AdDomain, out tempGuid))
244+
{
245+
var tempResult = context.AcquireToken(
246+
config.ResourceClientUri,
247+
config.ClientId,
248+
config.ClientRedirectUri,
249+
promptBehavior,
250+
UserIdentifier.AnyUser,
251+
AdalConfiguration.EnableEbdMagicCookie);
252+
config.AdDomain = tempResult.TenantId;
253+
context = CreateContext(config);
254+
promptBehavior = PromptBehavior.Never;
255+
}
256+
242257
result = context.AcquireToken(
243258
config.ResourceClientUri,
244259
config.ClientId,

src/ResourceManager/Profile/Commands.Profile.Test/LoginCmdletTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ public void LoginWithNoSubscriptionAndTenant()
180180
Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
181181
}
182182

183+
[Fact]
184+
[Trait(Category.RunType, Category.LiveOnly)]
185+
public void LoginWithNoSubscriptionAndTenantDomain()
186+
{
187+
var cmdlt = new ConnectAzureRmAccountCommand();
188+
// Setup
189+
cmdlt.CommandRuntime = commandRuntimeMock;
190+
cmdlt.TenantId = "microsoft.onmicrosoft.com";
191+
cmdlt.SetParameterSet("UserWithSubscriptionId");
192+
193+
// Act
194+
cmdlt.InvokeBeginProcessing();
195+
cmdlt.ExecuteCmdlet();
196+
cmdlt.InvokeEndProcessing();
197+
198+
Assert.NotNull(AzureRmProfileProvider.Instance.Profile.DefaultContext);
199+
Assert.Equal("microsoft.com", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Directory);
200+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", AzureRmProfileProvider.Instance.Profile.DefaultContext.Tenant.Id);
201+
}
202+
183203
[Fact]
184204
[Trait(Category.RunType, Category.LiveOnly)]
185205
public void LoginWithSubscriptionname()

src/ResourceManager/Profile/Commands.Profile/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
* Add user id to default context name to avoid context clashing
2222
- https://github.com/Azure/azure-powershell/issues/6489
2323
* Fix issues with Clear-AzureRmContext that caused issues with selecting a context #6398
24+
* Enable tenant domain to be passed to `-TenantId` parameter for `Connect-AzureRmAccount`
25+
- https://github.com/Azure/azure-powershell/issues/3974
26+
- https://github.com/Azure/azure-powershell/issues/6709
2427

2528
## Version 5.3.4
2629
* Updated Common.Strategy library to be able to validate that the current config for a resource is compatible with the target resource. Default is always true, individual resources and overridet the default.

src/ResourceManager/Profile/Commands.Profile/Models/RMProfileClient.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,24 @@ public AzureRmProfile Login(
147147
// (tenant is present and subscription is not provided)
148148
if (!string.IsNullOrEmpty(tenantId))
149149
{
150+
Guid tempGuid = Guid.Empty;
151+
if (!Guid.TryParse(tenantId, out tempGuid))
152+
{
153+
var tenant = ListAccountTenants(
154+
account,
155+
environment,
156+
password,
157+
promptBehavior,
158+
promptAction)?.FirstOrDefault();
159+
if (tenant == null || tenant.Id == null)
160+
{
161+
throw new ArgumentNullException(string.Format("Could not find tenant id for provided tenant domain '{0}'. Please ensure that " +
162+
"the provided service principal is found in the provided tenant domain.", tenantId));
163+
}
164+
165+
tenantId = tenant.Id;
166+
}
167+
150168
var token = AcquireAccessToken(
151169
account,
152170
environment,

0 commit comments

Comments
 (0)