Skip to content

Commit

Permalink
Move to using DefaultAzureCredential
Browse files Browse the repository at this point in the history
  • Loading branch information
cammj committed May 29, 2024
1 parent f6eff45 commit c659413
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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}");
Expand Down Expand Up @@ -419,18 +412,14 @@ private static async Task GetTenantSimulationUsers(GraphServiceClient GraphClien
/// <returns></returns>
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);
}

/// <summary>
Expand Down

0 comments on commit c659413

Please sign in to comment.