﻿# 如何透過命令列工具清除 Windows 認證管理員中的帳號密碼

最近嘗試使用 [Azure Static Web Apps CLI](https://azure.github.io/static-web-apps-cli/) 來開發與部署前端應用程式，但發現我用 `swa login` 之後，卻完全沒有「登出」的選項，經研究後發現 `swa` 把登入過程的認證資訊全部都放在 Windows 控制台的**認證管理員**(Credential Manager)之中。這篇文章我打算來分享如何透過 PowerShell 快速清除儲存在系統中的認證資訊。

![CMDKEY](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/e5dcf504426249d2e5f5-196738868-7ee4b478-ac7b-4478-bce3-227a9684d668.png)

### 安裝 Azure Static Web Apps CLI 並登入 Azure

1.  安裝 Node.js LTS
    
    ```bash
    choco install nodejs-lts -y
    ```
    
2.  安裝 Azure Static Web Apps CLI
    
    ```bash
    npm install -g @azure/static-web-apps-cli
    ```
    
3.  使用 `swa` 命令列工具登入 Azure 帳戶
    
    ```bash
    swa login
    ```
    
    過程會自動開啟瀏覽器，並進行 OAuth 2.0 授權流程，通過認證與授權後，基本上就會在 Windows 控制台的**認證管理員**(Credential Manager)寫入認證資訊。
    

### 如何清空 Azure 登入快取 (或任何儲存在裡面的任何認證資訊)

以下我們會透過 Windows 內建的 `cmdkey` 工具程式來幫助我們操作**認證管理員**中的資訊。

-   列出所有已儲存的認證清單(Credential List)
    
    ```powershell
    cmdkey.exe /list
    ```
    
    若是 `swa` 寫入的資料，其內容會長這樣：
    
    ```
        Target: LegacyGeneric:target=swa-cli-50a48d6ace789c15788810715b78db37/AzureCloud-1
        Type: Generic
        User: AzureCloud-1
    ```
    
    > 這裡的 `Target:` 是重要的識別資訊，未來要刪除該筆記錄必須使用這個值。
    
-   列出所有 `swa` 相關的認證清單(Credential List)
    
    ```powershell
    cmdkey.exe /list | findstr LegacyGeneric:target=swa-cli
    ```
    
    也可以寫簡單一點：
    
    ```powershell
    cmdkey.exe /list | findstr swa-cli
    ```
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/620c826637af88f092ee-196514250-6b6bff92-871e-4ce1-a4b0-9d882800d7e9.png)
    
-   刪除一筆紀錄
    
    ```powershell
    cmdkey.exe /delete:"LegacyGeneric:target=swa-cli-50a48d6ace789c15788810715b78db37/AzureCloud-1"
    ```
    
    畫面會顯示以下訊息：
    
    ```
    CMDKEY: Credential deleted successfully.
    ```
    
-   快速刪除所有 `swa` 建立的認證資料
    
    ```powershell
    cmdkey.exe /list | findstr swa-cli | ForEach-Object {
      cmdkey /delete:$($_.Split(' ')[5])
    }
    ```
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/7ee69d1ed8c5d2deb62d-196514354-6d59a9f8-3686-4c02-8f18-20b7d833dfd9.png)
    

### 總結

事實上，這個 `CMDKEY` 命令列工具支援**新增**與**刪除**認證，使用上也相當容易上手，相當不錯！

```batch
PS> cmdkey

Creates, displays, and deletes stored user names and passwords.

The syntax of this command is:

CMDKEY [{/add | /generic}:targetname {/smartcard | /user:username {/pass{:password}}} | /delete{:targetname | /ras} | /list{:targetname}]

Examples:

  To list available credentials:
     cmdkey /list
     cmdkey /list:targetname

  To create domain credentials:
     cmdkey /add:targetname /user:username /pass:password
     cmdkey /add:targetname /user:username /pass
     cmdkey /add:targetname /user:username
     cmdkey /add:targetname /smartcard

  To create generic credentials:
     The /add switch may be replaced by /generic to create generic credentials

  To delete existing credentials:
     cmdkey /delete:targetname

  To delete RAS credentials:
     cmdkey /delete /ras
```

### 相關連結

-   [cmdkey | Microsoft Learn](https://learn.microsoft.com/zh-tw/windows-server/administration/windows-commands/cmdkey?WT.mc_id=DT-MVP-4015686)
-   [Static Web Apps CLI Documentation | Static Web Apps CLI](https://azure.github.io/static-web-apps-cli/)
-   [GitHub - Azure/static-web-apps-cli: Azure Static Web Apps CLI ✨](https://github.com/Azure/static-web-apps-cli)
-   [Azure Identity client library for JavaScript (@azure/identity)](https://www.npmjs.com/package/@azure/identity)
