﻿# 當遇到 Visual Studio 2022 無法登入微軟個人帳戶的解決之道

這個問題已經困擾了我數個月，實在是覺得很厭煩，每次開啟 Visual Studio 2022 就會不斷的提醒我要不斷登入，然後還是登入不成功，上網找了非常多類似的問題，但沒有一個是有解決的。這篇文章我就來說說這個來龍去脈，並提供一個實證有效的解決方法。

![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/63ebb1a2bd9e89b7f679-209460062-30a5685d-1894-4d15-8ef6-88a146696fc3.png)

### 問題說明

這個問題是這樣的，當我們開啟 Visual Studio 2022 並嘗試登入自己的**微軟帳戶**(Microsoft Account)，例如像 Hotmail 帳號之類的。Visual Studio 2022 會先下載該帳號所有有加入的 Azure Active Directory (Azure AD) 組織 (Tenant)，然後逐一取得不同 Tenant (組織) 下的訂用帳戶。而這個過程如果遇到任何一個 Tenant 沒有存取權限，就會遇到這個問題。以下就是發生問題的重現步驟：

1.  先開啟 Visual Studio 2022 並點擊右上角的 Sign in 登入
    
    ![Visual Studio 2022 Sign in](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/ce802a892765e3ac4bc2-209458236-7d6dff78-d806-469d-90c5-4a5cd2b78008.png)
    
2.  輸入你的微軟帳戶(Microsoft Account)(個人帳戶)
    
    這個畫面其實跑得是 Internet Explorer 瀏覽器喔，真的很扯！😅
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/98adb660eff4bab40e13-209458250-6f549f59-0ba9-4a89-9614-dba6f2b4a786.png)
    
3.  因為我有啟用 MFA 驗證，所以要到手機的 [Microsoft Authenticator](https://support.microsoft.com/zh-tw/account-billing/%E4%B8%8B%E8%BC%89%E4%B8%A6%E5%AE%89%E8%A3%9D-microsoft-authenticator-%E6%87%89%E7%94%A8%E7%A8%8B%E5%BC%8F-351498fc-850a-45da-b7b6-27e523b8702a?WT.mc_id=DT-MVP-4015686) 授權登入
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/18482c12e04fdf60b8bf-209458257-d9122f49-b657-4ee9-8a82-6ac6490e2a00.png)
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/8c7bd0311ff6e38e7828-209458496-4cbf1195-6103-4fe4-b903-699dc68dcee0.png)
    
4.  此時 Visual Studio 2022 的登入視窗會關閉，會讓你感覺已經登入成功，但隨即會立刻再開啟一次視窗，叫你再登入一次！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/18482c12e04fdf60b8bf-209458257-d9122f49-b657-4ee9-8a82-6ac6490e2a00.png)
    
5.  理論上不會再需要登入了，但是我的 VS2022 卻會再次跳出視窗，叫我再登入一次，就時會出現錯誤了！
    
    他會提示我，我的帳號並沒有出現在 `OOOOOO` 的 tenant (租戶) (組織) 帳號下，所以無法存取 Visual Studio 應用程式。
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/7005adb35c1690aa6eec-209458523-9ede452a-bc5e-4e8e-871b-d65998e81e69.png)
    
    重點是，所謂 `OOOOOO` 這個 Tenant 我早就沒用了，那是之前曾經有一小段時間幫客戶代管過，他們後來自己拿回去管，然後把我的帳號從該 Tenant 的 Azure Active Directory 移除。理論上，我應該是沒有任何權限再看到他們的任何資料才對，但是他們很有可能是沒有把我從 Subscription (**訂用帳戶**) 的權限中移除，導致我還看的到**訂用帳戶**，但因為我不在他們的 Azure AD 中，所以其實還是沒有權限，但卻看的到他們的**訂用帳戶**與**資源群組清單**，裡面有多少資源我是看不到的，就是一個這麼尷尬的狀況！
    
    > 這很明顯是 Azure AD 的問題，我問了微軟技術支援中心，他們也不知道為什麼會這樣，正在積極幫我處理此議題。
    

### 解決方案

這個問題卡了我數月之久，每次看到都感覺厭煩，今天早上在家立誓要找到解法，沒有好心情怎麼能把 Visual Studio 用好呢！😠

最後我找到了解法，做出了兩項 \[工具\] -> \[選項\] 的設定調整：

1.  `Environment` > `Accounts` > `Add and reauthenticate accounts using:`
    
    這裡我直接把 Internet Explorer (IE) 瀏覽器換掉，直接改用系統內建的瀏覽器來進行登入！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/659c7b0ec9b255faa277-209458819-6f15e459-786a-44d6-a204-83d8f143cae2.png)
    
2.  `Environment` > `Accounts` > `Authenticate across all Azure Active Directories when adding account`
    
    這個是最關鍵的，因為該選項預設勾選，他會自動將你的帳號所有可以存取的 Azure Active Directory (Tenant) 全部都輪流驗證過一遍，這是為何你會一直不斷出現登入畫面的原因！一般來說，不應該一直不斷重複登入，但因為我有些「還看的到的 Azure AD 組織」已經「沒有權限」了，所以才會一直跳出登入畫面！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/d29421db180044c2b832-209458844-f9fcbf83-e6b0-49ff-8459-a51b03d38248.png)
    

只要設定完成，每次 Visual Studio 2022 開啟後就不會一直叫你登入了！

### 徹底清空 Visual Studio 2022 的認證快取

如果你還是遇到問題，那可能會想要徹底清空 Visual Studio 2022 的認證快取，請依照以下步驟操作：

1.  關閉 Visual Studio 2022 開發工具
    
2.  找到 `%LocalAppData%\.IdentityService` 目錄
    
3.  刪除 `%LocalAppData%\.IdentityService` 資料夾
    
4.  重開 Visual Studio 2022 之後會自動將該資料夾重建
    

### 選擇你要在 Visual Studio 2022 裡面使用哪些訂用帳戶

但由於我們選擇了不要在所有 Azure Active Directories 重新認證，這意味的你可能要自己選擇有哪些訂閱要加入到 Visual Studio 2022 之中，請參考下圖進行操作即可：

1.  進入**帳戶設定** (Account settings)
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/88fcddef5be29351fd9f-209459604-c16b400c-64ba-4212-a5e7-2504e8673b74.png)
    
2.  選則 `[Multiple] Microsoft account` 的 `Apply filter...`
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/33b0981c3241cab39db9-209459432-dfda1cbe-3ab1-4daf-89d6-61febd79fd72.png)
    
3.  選擇你會用到的訂用帳戶
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/726f10aed658b90f44ca-209459455-3725054c-72f3-4828-bd22-1fd29057a74e.png)
    

### 其他技術筆記

-   指明登入特定 Tenant 的網址
    
    假設你的 Tenant 的 Domain 是 `myorg.onmicrosoft.com` 的話，你可以使用下面的網址來登入：
    
    [https://myaccount.microsoft.com/?tenantId=myorg.onmicrosoft.com](https://myaccount.microsoft.com/?tenantId=myorg.onmicrosoft.com)
    
    如果你知道你的 `TenantId` 卻切的值為何，可以直接使用下面的網址來登入：
    
    [https://myaccount.microsoft.com/?tenantId=4e7aed31-e502-4e30-a52f-90452c103e8c](https://myaccount.microsoft.com/?tenantId=4e7aed31-e502-4e30-a52f-90452c103e8c)
    
-   查詢目前帳戶可以已經加入的組織清單 (Tenant List)
    
    以前 `myaccount.microsoft.com` 這個網站可以支援**微軟個人帳戶**登入使用，現在不能用了！
    
    [https://myaccount.microsoft.com/organizations](https://myaccount.microsoft.com/organizations)
    
    > 所以我的 [Microsoft 帳戶如何才能脫離其他人的 Azure AD 目錄/組織](/post/2018/10/19/How-to-leave-others-Azure-AD-by-yourself) 文章目前是失效的！
    

### 相關連結

-   [Issues signing in to Visual Studio subscriptions](https://learn.microsoft.com/en-us/visualstudio/subscriptions/sign-in-issues?WT.mc_id=DT-MVP-4015686)
-   [How to nuke the Identity Cache in Visual Studio?](https://www.koskila.net/how-to-nuke-the-identity-cache-in-visual-studio/)
-   [The portal is having issues getting an authentication token. The experience rendered may be degraded.](https://learn.microsoft.com/en-us/answers/questions/636026/the-portal-is-having-issues-getting-an-authenticat-1.html?WT.mc_id=DT-MVP-4015686)
-   [How to leave an Azure AD tenant?](https://www.koskila.net/how-to-remove-your-account-from-guest-aad/)
-   [Leave an organization as an external user](https://learn.microsoft.com/en-us/azure/active-directory/external-identities/leave-the-organization?WT.mc_id=DT-MVP-4015686)
