Skip to content

Instantly share code, notes, and snippets.

@asadrefai
Last active July 7, 2021 14:50
Show Gist options
  • Save asadrefai/b40b95bb4efc827c1402f3d7c85ae46b to your computer and use it in GitHub Desktop.
Save asadrefai/b40b95bb4efc827c1402f3d7c85ae46b to your computer and use it in GitHub Desktop.
Gets the secret value from Azure Key Vault and establish SharePoint connection
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
const string secretName = "Test";
var keyVaultName = "test-blog-kv";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
var secret = await client.GetSecretAsync(secretName);
var cert = new X509Certificate2(Convert.FromBase64String(secret.Value.Value));
var cc = new PnP.Framework.AuthenticationManager("<Azure App Registration Client ID>", cert, "<Tenant ID>", null, PnP.Framework.AzureEnvironment.Production, null).GetContext("https://Tenant.sharepoint.com");
cc.Load(cc.Web);
cc.ExecuteQuery();
return new OkObjectResult("Title of web: " + cc.Web.Title);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment