-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathExample.cs
51 lines (42 loc) · 1.67 KB
/
Example.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
using System;
using System.Collections.Generic;
using VaultSharp;
using VaultSharp.V1.AuthMethods;
using VaultSharp.V1.AuthMethods.Token;
using VaultSharp.V1.Commons;
// This is the accompanying code for the Developer Quick Start.
// WARNING: Using root tokens is insecure and should never be done in production!
namespace DeveloperQuickstart
{
class Program
{
static void Main(string[] args)
{
// Authentication
IAuthMethodInfo authMethod = new TokenAuthMethodInfo(vaultToken: "dev-only-token");
VaultClientSettings vaultClientSettings = new VaultClientSettings("http://127.0.0.1:8200", authMethod);
IVaultClient vaultClient = new VaultClient(vaultClientSettings);
// Writing a secret
var secretData = new Dictionary<string, object> { { "password", "Hashi123" } };
vaultClient.V1.Secrets.KeyValue.V2.WriteSecretAsync(
path: "/my-secret-password",
data: secretData,
mountPoint: "secret"
).Wait();
Console.WriteLine("Secret written successfully.");
// Reading a secret
Secret<SecretData> secret = vaultClient.V1.Secrets.KeyValue.V2.ReadSecretAsync(
path: "/my-secret-password",
mountPoint: "secret"
).Result;
var password = secret.Data.Data["password"];
if (password.ToString() != "Hashi123")
{
throw new System.Exception("Unexpected password");
}
Console.WriteLine("Access granted!");
}
}
}