Skip to content

Commit fa4089e

Browse files
erich-wangisra-fel
andauthored
Fix device code authentication with tenantId (#13482)
* Fix the issue that tenantId is not respected during device code * Warning message refine * add change log Co-authored-by: Yeming Liu <[email protected]>
1 parent df875b0 commit fa4089e

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/Accounts/Accounts/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Fixed an issue that TenantId may be not respected if using `Connect-AzAccount -DeviceCode`[#13477]
2122
* Added new cmdlet `Get-AzAccessToken`
2223
* Fixed an issue that error happens if user profile path is inaccessible
2324
* Fixed an issue causing Write-Object error during Connect-AzAccount [#13419]

src/Accounts/Accounts/Models/RMProfileClient.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ public AzureRmProfile Login(
195195
environment,
196196
subscriptionId,
197197
subscriptionName,
198+
true,
198199
out newSubscription,
199200
out newTenant))
200201
{
@@ -248,7 +249,7 @@ public AzureRmProfile Login(
248249

249250
if (token != null &&
250251
newTenant == null &&
251-
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, out tempSubscription, out tempTenant))
252+
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant))
252253
{
253254
// If no subscription found for the given token/tenant,discard tempTenant value.
254255
// Continue to look for matched subscripitons until one subscription retrived by its home tenant is found.
@@ -554,6 +555,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
554555
IAzureEnvironment environment,
555556
string subscriptionId,
556557
string subscriptionName,
558+
bool isTenantPresent,
557559
out IAzureSubscription subscription,
558560
out IAzureTenant tenant)
559561
{
@@ -596,7 +598,13 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
596598
}
597599
catch (CloudException ex)
598600
{
599-
WriteWarningMessage(ex.Message);
601+
//Error "InvalidAuthenticationTokenTenant" means tenant and subscription mismatches.
602+
//If tenant is not present, we're iterating all tenants until finding right tenant for specified subscription,
603+
//in this case, InvalidAuthenticationTokenTenant message is expected and we should ignore it.
604+
if (isTenantPresent || !string.Equals(ex.Body?.Code, "InvalidAuthenticationTokenTenant", StringComparison.OrdinalIgnoreCase))
605+
{
606+
WriteWarningMessage(ex.Message);
607+
}
600608
}
601609

602610
if (subscription != null)

src/Accounts/Authenticators/DeviceCodeAuthenticator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public override Task<IAccessToken> Authenticate(AuthenticationParameters paramet
4848
DeviceCodeCallback = DeviceCodeFunc,
4949
AuthorityHost = new Uri(authority),
5050
ClientId = clientId,
51-
TenantId = onPremise ? tenantId : null,
51+
TenantId = tenantId,
5252
TokenCache = tokenCache.TokenCache,
5353
};
5454
var codeCredential = new DeviceCodeCredential(options);

0 commit comments

Comments
 (0)