From c659413a0d06797dd1e11354104822488d47de3b Mon Sep 17 00:00:00 2001 From: Cam Date: Wed, 29 May 2024 12:08:32 +1000 Subject: [PATCH] Move to using DefaultAzureCredential --- Sync.cs | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/Sync.cs b/Sync.cs index 559e8fb..6f379c6 100644 --- a/Sync.cs +++ b/Sync.cs @@ -18,10 +18,6 @@ namespace ASTSync; public static class Sync { - private static string _appClientId = Environment.GetEnvironmentVariable("AppClientId", EnvironmentVariableTarget.Process); - private static string _appTenantId = Environment.GetEnvironmentVariable("AppTenantId", EnvironmentVariableTarget.Process); - private static string _appSecret = Environment.GetEnvironmentVariable("AppSecret", EnvironmentVariableTarget.Process); - // If to pull entra users private static bool _pullEntraUsers = bool.Parse(Environment.GetEnvironmentVariable("SyncEntra", EnvironmentVariableTarget.Process) ?? "false"); @@ -67,10 +63,7 @@ public static async Task RunAsync([TimerTrigger("0 */15 * * * *")] TimerInfo myT _log = log; - // Validate required variables - if (string.IsNullOrEmpty(_appClientId) || string.IsNullOrEmpty(_appTenantId) || string.IsNullOrEmpty(_appSecret)) - throw new Exception("AppClientID, AppTenantID, and AppSecret must be set"); - + // Get graph client var GraphClient = GetGraphServicesClient(); _log.LogInformation($"C# Timer trigger function executed at: {DateTime.UtcNow}"); @@ -419,18 +412,14 @@ private static async Task GetTenantSimulationUsers(GraphServiceClient GraphClien /// private static GraphServiceClient GetGraphServicesClient() { - // Construct auth provider to Graph - var scopes = new[] { "https://graph.microsoft.com/.default" }; - var tenantId = "common"; + // Use default azure credential + var tokenCredential = new DefaultAzureCredential(); - var options = new TokenCredentialOptions - { - AuthorityHost = AzureAuthorityHosts.AzurePublicCloud - }; + // Default graph scope + var scopes = new[] { "https://graph.microsoft.com/.default" }; - var clientSecretCredential = new ClientSecretCredential(_appTenantId, _appClientId, _appSecret, options); - - return new GraphServiceClient(clientSecretCredential, scopes); + // Return graph services client + return new GraphServiceClient(tokenCredential, scopes); } ///