Azure 密钥保管库(Azure Key Vault)是一种云服务,用于集中存储和管理这些敏感信息,下面介绍如何使用 VB.NET 从 Azure 密钥保管库安全地获取密钥值。
1. 引入命名空间
引入与Azure密钥保管库交互所需的命名空间。
Imports Azure.Identity
Imports Azure.Security.KeyVault.Secrets
2. 完善 Main 方法
在 Main 方法中调用异步方法获取密钥值。
Module Program
Public Sub Main()
Try
Dim secretValue = GetSecretAsync().GetAwaiter().GetResult()
Console.WriteLine($"Secret value: {secretValue}")
Catch ex As Exception
Console.WriteLine($"Error retrieving secret: {ex.Message}")
End Try
End Sub
End Module
3. 通过异步函数获取密钥
Private Async Function GetSecretAsync() As Task(Of String)
Const KEY_VAULT_NAME As String = "samplevault326"
Const KEY_NAME As String = "secret1"
Dim kvUri = $"https://{KEY_VAULT_NAME}.vault.azure.net"
Dim credential = New DefaultAzureCredential()
Dim client = New SecretClient(New Uri(kvUri), credential)
Dim secretResponse = Await client.GetSecretAsync(KEY_NAME)
If secretResponse Is Nothing OrElse secretResponse.Value Is Nothing Then
Throw New Exception("Secret not found")
Else
Return secretResponse.Value.Value
End If
End Function