This page shows how to call the GetSecretValue API using the KMS instance SDK client in C#. Before running the examples, initialize the client.
Prerequisites
Before you begin, ensure that you have:
A KMS instance with at least one secret created
The KMS instance SDK installed in your C# project
The Client Key file and its password stored as environment variables (
ClientKeyFileandPassword)The KMS instance CA certificate file path and endpoint (
<KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com)
Complete example
Example walkthrough
Step 1: Initialize the client
Configure the KMS instance connection using your Client Key, CA certificate, and endpoint, then create the SDK client.
string regionId = "<REGION_ID>";
// The CA certificate of the KMS instance.
string caFilePath = "<CA_CERTIFICATE>";
// Set the endpoint to <your KMS Instance Id>.cryptoservice.kms.aliyuncs.com.
string endpoint = "<ENDPOINT>";
// Set the Client Key and the security token of the Client Key.
AlibabaCloud.Dkms.Gcs.OpenApi.Models.Config kmsInstanceConfig = CreateKmsInstanceConfig(AlibabaCloud.DarabonbaEnv.Client.GetEnv("ClientKeyFile"), AlibabaCloud.DarabonbaEnv.Client.GetEnv("Password"), endpoint, caFilePath);
AlibabaCloud.Dkms.Gcs.Sdk.Client client = CreateClient(kmsInstanceConfig);For the full client initialization reference, see Initialize the client.
Step 2: Call the GetSecretValue API
Build a GetSecretValueRequest with the secret name, version stage, and whether to fetch extended configuration, then call GetSecretValue on the client.
public static AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueResponse GetSecretValue(AlibabaCloud.Dkms.Gcs.Sdk.Client client, string secretName, string versionStage, bool? fetchExtendedConfig)
{
AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest request = new AlibabaCloud.Dkms.Gcs.Sdk.Models.GetSecretValueRequest
{
SecretName = secretName,
VersionStage = versionStage,
FetchExtendedConfig = fetchExtendedConfig,
};
//Ignore CA certificate authentication.
//AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions runtime = new AlibabaCloud.Dkms.Gcs.OpenApiUtil.Models.RuntimeOptions();
//runtime.IgnoreSSL = true;
//return client.GetSecretValueWithOptions(request,runtime);
return client.GetSecretValue(request);
}For the full API reference, see GetSecretValue.
Request parameters
| Parameter | Type | Description |
|---|---|---|
SecretName | string | The name of the secret to retrieve. |
VersionStage | string | The version stage of the secret value. |
FetchExtendedConfig | bool? | Specifies whether to fetch the extended configuration of the secret. |
Usage notes
Store your Client Key file path and password as environment variables (
ClientKeyFileandPassword) rather than hardcoding them in source code.The commented-out
RuntimeOptionsblock shows how to skip CA certificate authentication (IgnoreSSL = true) for testing. Do not disable SSL verification in production.To call the API asynchronously, use
GetSecretValueAsyncinstead ofGetSecretValue.