Skip to content

Instantly share code, notes, and snippets.

@aramase
Last active April 29, 2021 06:37
Show Gist options
  • Select an option

  • Save aramase/c877c6a8840d25213240ec4b876bf5f5 to your computer and use it in GitHub Desktop.

Select an option

Save aramase/c877c6a8840d25213240ec4b876bf5f5 to your computer and use it in GitHub Desktop.
using Microsoft.Identity.Client;
using System;
using System.Threading;
using System.Collections.Generic;
namespace aramase.test
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Testing OIDC with MSAL .NET!");
var client_id = Environment.GetEnvironmentVariable("AZURE_CLIENT_ID");
var token_path = Environment.GetEnvironmentVariable("TOKEN_FILE_PATH");
var tenant_id = Environment.GetEnvironmentVariable("AZURE_TENANT_ID");
while (true)
{
Console.WriteLine($"{Environment.NewLine}START {DateTime.UtcNow} ({Environment.MachineName})");
Console.WriteLine($"client id: {client_id}");
Console.WriteLine($"tenant id: {tenant_id}");
GetTokenAsync(token_path, client_id, tenant_id);
Thread.Sleep(600000);
}
}
static async void GetTokenAsync(string token_path, string client_id, string tenant_id)
{
string signedClientAssertion = ReadJWTFromFS(token_path);
string[] scopes = new string[] { "https://graph.microsoft.com/.default" };
Dictionary<string, string> otherParams = new Dictionary<string, string>();
otherParams.Add("dc", "<replace with dc for testing>");
otherParams.Add("fiextoidc", "true");
IConfidentialClientApplication confidentialClient;
confidentialClient = ConfidentialClientApplicationBuilder.Create(client_id).WithClientAssertion(signedClientAssertion).WithTenantId(tenant_id).Build();
AuthenticationResult result = await confidentialClient.AcquireTokenForClient(scopes).WithExtraQueryParameters(otherParams).ExecuteAsync();
Console.WriteLine($"bearer token: {result.AccessToken}");
Console.WriteLine($"token expiry: {result.ExpiresOn}");
}
static string ReadJWTFromFS(string token_path)
{
string text = System.IO.File.ReadAllText(token_path);
return text;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment