azure-sdk-for-cpp/samples/integration/cmake-vcpkg/main.cpp
Ronnie Geraghty 382efbd7fd
Moving sample files to cpp sdk repo (#5947)
* Moving sample files to cpp sdk repo

* Fixed spelling and markdown formatting

* Fixes for links

* added new lines to the end of files

* Update samples/integration/cmake-vcpkg/.gitignore

Co-authored-by: Rick Winter <rick.winter@microsoft.com>

* Update samples/integration/cmake-vcpkg/README.md

Co-authored-by: Rick Winter <rick.winter@microsoft.com>

* Update samples/integration/cmake-vcpkg/README.md

Co-authored-by: Rick Winter <rick.winter@microsoft.com>

* Update samples/integration/cmake-vcpkg/README.md

Co-authored-by: Rick Winter <rick.winter@microsoft.com>

* updates from feedback

* Updates to sample code to use default azure cred

* Update samples/integration/cmake-vcpkg/main.cpp

Co-authored-by: Rick Winter <rick.winter@microsoft.com>

---------

Co-authored-by: Rick Winter <rick.winter@microsoft.com>
2024-09-13 15:29:29 -07:00

42 lines
1.3 KiB
C++

#include <azure/identity.hpp>
#include <azure/keyvault/secrets.hpp>
#include <iostream>
using namespace Azure::Security::KeyVault::Secrets;
int main()
{
std::cout << "Starting Program!" << std::endl;
try
{
// Set Key Vault URL string
auto const keyVaultUrl = std::getenv("AZURE_KEYVAULT_URL");
// Create Default Azure Credential to Authenticate.
// It will pick up on our AzureCLI login
auto credential = std::make_shared<Azure::Identity::DefaultAzureCredential>();
// Create Key Vault Secret Client
SecretClient secretClient(keyVaultUrl, credential);
// Create a Secret
std::string secretName("MySampleSecret");
std::string secretValue("My super secret value");
secretClient.SetSecret(secretName, secretValue);
// Get the Secret
KeyVaultSecret secret = secretClient.GetSecret(secretName).Value;
std::string valueString = secret.Value.HasValue() ? secret.Value.Value() : "NONE RETURNED";
std::cout << "Secret is returned with name " << secret.Name << " and value " << valueString
<< std::endl;
}
catch (const Azure::Core::RequestFailedException& ex)
{
std::cout << std::underlying_type<Azure::Core::Http::HttpStatusCode>::type(ex.StatusCode)
<< std::endl;
}
std::cout << "End of Program!" << std::endl;
}