Skip to content

Fix device code authentication with tenantId #13482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Accounts/Accounts/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Additional information about change #1
-->
## Upcoming Release
* Fixed an issue that TenantId may be not respected if using `Connect-AzAccount -DeviceCode`[#13477]
* Added new cmdlet `Get-AzAccessToken`
* Fixed an issue that error happens if user profile path is inaccessible
* Fixed an issue causing Write-Object error during Connect-AzAccount [#13419]
Expand Down
12 changes: 10 additions & 2 deletions src/Accounts/Accounts/Models/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ public AzureRmProfile Login(
environment,
subscriptionId,
subscriptionName,
true,
out newSubscription,
out newTenant))
{
Expand Down Expand Up @@ -248,7 +249,7 @@ public AzureRmProfile Login(

if (token != null &&
newTenant == null &&
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, out tempSubscription, out tempTenant))
TryGetTenantSubscription(token, account, environment, subscriptionId, subscriptionName, false, out tempSubscription, out tempTenant))
{
// If no subscription found for the given token/tenant,discard tempTenant value.
// Continue to look for matched subscripitons until one subscription retrived by its home tenant is found.
Expand Down Expand Up @@ -554,6 +555,7 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
IAzureEnvironment environment,
string subscriptionId,
string subscriptionName,
bool isTenantPresent,
out IAzureSubscription subscription,
out IAzureTenant tenant)
{
Expand Down Expand Up @@ -596,7 +598,13 @@ private bool TryGetTenantSubscription(IAccessToken accessToken,
}
catch (CloudException ex)
{
WriteWarningMessage(ex.Message);
//Error "InvalidAuthenticationTokenTenant" means tenant and subscription mismatches.
//If tenant is not present, we're iterating all tenants until finding right tenant for specified subscription,
//in this case, InvalidAuthenticationTokenTenant message is expected and we should ignore it.
if (isTenantPresent || !string.Equals(ex.Body?.Code, "InvalidAuthenticationTokenTenant", StringComparison.OrdinalIgnoreCase))
{
WriteWarningMessage(ex.Message);
}
}

if (subscription != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Accounts/Authenticators/DeviceCodeAuthenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override Task<IAccessToken> Authenticate(AuthenticationParameters paramet
DeviceCodeCallback = DeviceCodeFunc,
AuthorityHost = new Uri(authority),
ClientId = clientId,
TenantId = onPremise ? tenantId : null,
TenantId = tenantId,
TokenCache = tokenCache.TokenCache,
};
var codeCredential = new DeviceCodeCredential(options);
Expand Down