共用方式為


使用 JavaScript 在 Azure Key Vault 中啟用和停用秘密

使用適當的程式設計驗證認證建立 SecretClient,然後使用用戶端從 Azure Key Vault 啟用和停用秘密。

啟用密鑰

若要在 Azure Key Vault 中啟用秘密,請使用 SecretClient 類別的 updateSecretProperties 方法。

const name = 'mySecret';
const version= 'd9f2f96f120d4537ba7d82fecd913043'

const properties = await client.updateSecretProperties(
    secretName,
    version,
    { enabled: true }
);

// get secret value
const { value } = await client.getSecret(secretName, version);

這個方法會傳回 SecretProperties 物件。

停用新的祕密

若要在建立秘密時關閉它,請使用 setSecret 方法,並將選項中的 啟用 設定為 false。

const mySecretName = 'mySecret';
const mySecretValue = 'mySecretValue';

// Success
const { name, value, properties } = await client.setSecret(
    mySecretName, 
    mySecretValue, 
    { enabled: false }
);

// Can't read value of disabled secret
try{
    const secret = await client.getSecret(
        mySecretName, 
        properties.version
    );
} catch(err){
    // `Operation get is not allowed on a disabled secret.`
    console.log(err.message);
}

停用現有的機密

若要停用 Azure Key Vault 中的現有秘密,請使用 SecretClient 類別的 updateSecretProperties 方法。

const name = 'mySecret';
const version= 'd9f2f96f120d4537ba7d82fecd913043';

// Success
const properties = await client.updateSecretProperties(
    secretName,
    version,
    { enabled: false }
);

// Can't read value of disabled secret
try{
    const { value } = await client.getSecret(secretName, version);
} catch(err){
    // `Operation get is not allowed on a disabled secret.`
    console.log(err.message);
}

這個方法會傳回 SecretProperties 物件。

後續步驟