Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Last active July 7, 2021 14:51
Show Gist options
  • Save asadrefai/1fa78004235c2550d255f8e879cdedb9 to your computer and use it in GitHub Desktop.
Save asadrefai/1fa78004235c2550d255f8e879cdedb9 to your computer and use it in GitHub Desktop.
Gets the key vault secret value and establish connection with SharePoint in net core console app
static async Task Main(string[] args)
{
const string secretName = "Test";
var keyVaultName = "test-blog-kv";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions();
options.ExcludeEnvironmentCredential = true;
options.ExcludeInteractiveBrowserCredential = true;
options.ExcludeManagedIdentityCredential = true;
options.ExcludeSharedTokenCacheCredential = true;
options.ExcludeVisualStudioCodeCredential = true;
options.ExcludeVisualStudioCredential = true;
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential(options));
var secret = await client.GetSecretAsync(secretName);
var cert = new X509Certificate2(Convert.FromBase64String(secret.Value.Value));
var cc = new PnP.Framework.AuthenticationManager("<App Registration Client ID>", cert, "<Tenant ID>", null, PnP.Framework.AzureEnvironment.Production, null).GetContext("https://Tenant.sharepoint.com/");
cc.Load(cc.Web);
cc.ExecuteQuery();
Console.WriteLine(cc.Web.Title);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment