简体   繁体   中英

az cli logging into wrong account

I have az cli version 2.44.1 installed. When I perform az login from the command, I get prompted to login on the browser. I then proceed to use my account x@abc.com . Once the login on the browser is successful, my command prompt shows a message saying "No subscriptions found for y@abc.com ". For some reason, azure cli is picking up a different account (the account I'm logged on the PC as) instead of the account I'm selecting in the browser.

I have deleted the contents of .azure folder and redone the steps (hoping to flush the stored credentials) but the result seems to be the same; despite of me selecting x@abc.com on the logon window, I see that y@abc.com is selected by the azure CLI.

One workaround I have is: I can run the cmd.exe as a different user x@abc.com and then run az login from there. This way seems to pick the right account. But I'd like to see if there's a way to fix it some other way because I'd like to use az cli alongside terraform in my VSCode bash. I'm running Windows 10 Enterprise.

It seems that the issue is related to the credentials stored on your Windows machine. One solution could be to clear the Azure Active Directory (AAD) token cache by running the following command in the command prompt:

az account clear

This command will clear the AAD token cache, which will force the CLI to prompt you to login again and select the correct account.

Another solution would be to check the environment variables on your machine to ensure that the correct tenant and subscription are being used. You can check the tenant and subscription by running the following commands:

az account show
az account list

If the wrong tenant or subscription is being used, you can set the correct one by running the following commands:

az account set --subscription="SUBSCRIPTION_ID"
az account set --tenant="TENANT_ID"

You can also check and manage the stored credentials in the Credential Manager on your Windows machine.

It is also possible that there is a problem with the installed version of Azure CLI, you could try to update or reinstall it.

I got the same error when I tried it previously in my environment, and I found the issue.

在此处输入图像描述

Reason:

" No subscriptions found " indicates that the account has gone through authentication and does not have access to the subscriptions .

Go to Azure -> All Services > Subscriptions or search for subscription in the search panel, then visit Access control (IAM) and assign the "contributor" role as shown:

Path:

在此处输入图像描述

Script:

az account set --subscription <subscription> //not manadatory
az login --service-principal --username <APPID> --password <clientsecret> --tenant <>tenantID 

Logged in successfully:

在此处输入图像描述

Parallelly,

Upgrade CLI once by giving:

az upgrade

to check your "CLI is upto date".

在此处输入图像描述

Execute the command below to check the cli login details for future use.

az login --debug

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM