Import-AzureRMContext does not show any subscription
Description
When importing credentials from JSON file, I cannot enumerate my subscriptions
The script uses multiple Contexts/subscriptions, but in this version only one is active. (Logon is performed again when needed). Multiple contexts are saved in JSON files. Since AzureRM 6.0 it worked perfectly, but a recent change (probably 6.7.0) broke it.
Module Version
6.7.0
Script/Steps for Reproduction
Minimum repro code, as suggested by @markcowl :
Clear-AzureRMContext
Disable-AzureRMContextAutosave -Scope CurrentUser
$File = "C:\Bats\logon\jc_arm.json"
Import-AzureRMContext -Path $File -verbose
Get-AzureRMSubscription # -WarningAction SilentlyContinue
Prior to the change, I got some warnings related to old test directories, but also the two subscriptions existing, which I can select from. Now I'm getting just the warnings (and no subscriptions)
WARNING: Unable to acquire token for tenant 'a856b482-8519-4324-96aa-93aad8b23b6a' ... WARNING: Unable to acquire token for tenant 'd626b9b9-92ab-45dc-a2d4-9016dfbcc96b'
Test 1 : Removing the JSON file, creating it again (Connect-AzureRM +Save-AzureRMContext), then Get-AzureRMSubscription, I can see my 2 subscriptions. As I'm reading my own words, I believe it's just an interactive logon, the JSON file is not read in this case.
Debug Output
Test 2. Running the repro code with Get-AzureRMSubscription -Debug. During the Token Acquisition phase, Interesting ErrorCode: user_interaction_required
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
DEBUG: 08/21/2018 07:57:36: 733cfda7-05cf-4265-a806-d1936eb2819e - TokenCache: Looking up cache for a token...
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Information: 2 :
DEBUG: 08/21/2018 07:57:36: 733cfda7-05cf-4265-a806-d1936eb2819e - TokenCache: No matching token was found in the cache
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
DEBUG: 08/21/2018 07:57:36: - WindowsFormsWebAuthenticationDialogBase: Navigating to
'https://login.microsoftonline.com/a856b482-8519-4324-96aa-93aad8b23b6a/oauth2/authorize?resource=https://management.core.windows.net/&client_id=1950a258-227b-4e31-a9cf-717495945fc2&response_type=code&haschro
me=1&redirect_uri=urn:ietf:wg:oauth:2.0:oob&[email protected]&client-request-id=733cfda7-05cf-4265-a806-d1936eb2819e&prompt=attempt_none&x-client-SKU=.NET&x-client-Ver=2.28.3.860&x-cl
ient-CPU=x64&x-client-OS=Microsoft Windows NT 10.0.17134.0&site_id=501358&display=popup'.
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Verbose: 1 :
DEBUG: 08/21/2018 07:57:37: - WindowsFormsWebAuthenticationDialogBase: Navigating to
'https://login.microsoftonline.com/a856b482-8519-4324-96aa-93aad8b23b6a/oauth2/authorize?resource=https://management.core.windows.net/&client_id=1950a258-227b-4e31-a9cf-717495945fc2&response_type=code&haschro
me=1&redirect_uri=urn:ietf:wg:oauth:2.0:oob&[email protected]&client-request-id=733cfda7-05cf-4265-a806-d1936eb2819e&prompt=attempt_none&x-client-SKU=.NET&x-client-Ver=2.28.3.860&x-cl
ient-CPU=x64&x-client-OS=Microsoft Windows NT
10.0.17134.0&site_id=501358&display=popup&sso_nonce=AQABAAAAAADXzZ3ifr-GRbDT45zNSEFEI_eE0-_e2EO9SxZvI2SUHHyCfGqa1VfY6-ZOfm9TK8YcoesE94JoPuyfCAcmeFJgraZH-74D-mrmN_vJWRUCbCAA&mscrid=733cfda7-05cf-4265-a806-d193
6eb2819e'.
DEBUG: Microsoft.IdentityModel.Clients.ActiveDirectory Error: 4 :
DEBUG: 08/21/2018 07:57:39: 733cfda7-05cf-4265-a806-d1936eb2819e -
Hi Maddie, Please check https://github.com/Azure/azure-powershell/issues/7110, I've found a workaround which seems to work with 6.8.1. Now it wold be great to find a more logical/evident way to fix this issue... and probably document which .NET object can be used to see the cache again (as the previously used method now always returns $null) jc
$cache= [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared $cache.ReadItems()
Now empty in 6.7.x and 6.8.x
@juancrl TokenCache.DefautShared is not the place that we look by default for tokens, we use
[Microsoft.Azure.Commands.Common.Authentication.AzureSession]::Instance.TokenCache
However, this is not public-facing PowerShell API, and so, not recommended as a mechanism for getting data, as the specifics may change in the future.
Thanks ! I was now using (Get-AzureRMContext).TokenCache which probably points to the same place...