文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 AWS SDK 範例
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
搭配使用 CreateVirtualMfaDevice
與 CLI
下列程式碼範例示範如何使用 CreateVirtualMfaDevice
。
- CLI
-
- AWS CLI
-
若要建立虛擬 MFA 裝置
此範例會建立名為
BobsMFADevice
的新虛擬 MFA 裝置。它會建立包含名為QRCode.png
的引導資訊的檔案,並將其放至C:/
目錄中。此範例中使用的引導方法為QRCodePNG
。aws iam create-virtual-mfa-device \ --virtual-mfa-device-name
BobsMFADevice
\ --outfileC:/QRCode.png
\ --bootstrap-methodQRCodePNG
輸出:
{ "VirtualMFADevice": { "SerialNumber": "arn:aws:iam::210987654321:mfa/BobsMFADevice" }
如需詳細資訊,請參閱《AWS IAM 使用者指南》中的在 AWS中使用多重要素驗證 (MFA)。
-
如需 API 詳細資訊,請參閱 AWS CLI Command Reference 中的 CreateVirtualMfaDevice
。
-
- PowerShell
-
- Tools for PowerShell
-
範例 1:此範例會建立新的虛擬 MFA 裝置。第 2 行和第 3 行擷取虛擬 MFA 軟體程式建立帳戶所需的
Base32StringSeed
值 (做為 QR 程式碼的替代方案)。使用值設定程式後,依序從程式取得兩個驗證碼。最後,使用最後一個命令將虛擬 MFA 裝置連結至 IAM 使用者Bob
,並使用兩個驗證碼同步帳戶。$Device = New-IAMVirtualMFADevice -VirtualMFADeviceName BobsMFADevice $SR = New-Object System.IO.StreamReader($Device.Base32StringSeed) $base32stringseed = $SR.ReadToEnd() $base32stringseed CZWZMCQNW4DEXAMPLE3VOUGXJFZYSUW7EXAMPLECR4NJFD65GX2SLUDW2EXAMPLE
輸出:
-- Pause here to enter base-32 string seed code into virtual MFA program to register account. -- Enable-IAMMFADevice -SerialNumber $Device.SerialNumber -UserName Bob -AuthenticationCode1 123456 -AuthenticationCode2 789012
範例 2:此範例會建立新的虛擬 MFA 裝置。第 2 行和第 3 行會擷取
QRCodePNG
值,並將其寫入檔案。此映像可由虛擬 MFA 軟體程式掃描,以建立帳戶 (做為手動輸入 Base32StringSeed 值的替代方案)。在虛擬 MFA 程式中建立帳戶後,請依序取得兩個驗證碼,並在最後一個命令中輸入它們,將虛擬 MFA 裝置連結至 IAM 使用者Bob
並同步帳戶。$Device = New-IAMVirtualMFADevice -VirtualMFADeviceName BobsMFADevice $BR = New-Object System.IO.BinaryReader($Device.QRCodePNG) $BR.ReadBytes($BR.BaseStream.Length) | Set-Content -Encoding Byte -Path QRCode.png
輸出:
-- Pause here to scan PNG with virtual MFA program to register account. -- Enable-IAMMFADevice -SerialNumber $Device.SerialNumber -UserName Bob -AuthenticationCode1 123456 -AuthenticationCode2 789012
-
如需 API 詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet Reference 中的 CreateVirtualMfaDevice。
-