Skip to content

Instantly share code, notes, and snippets.

@jongio
Created March 10, 2020 21:22
Show Gist options
  • Save jongio/d3682511f715ada6681363a0fe7ad8ee to your computer and use it in GitHub Desktop.
Save jongio/d3682511f715ada6681363a0fe7ad8ee to your computer and use it in GitHub Desktop.
using System;
using System.Threading.Tasks;
using Microsoft.Identity.Client;
using System.Text;
using System.Globalization;
using Microsoft.Azure.Management.DataFactory;
using Microsoft.Rest;
using Azure.Security.KeyVault.Secrets;
using Azure.Identity;
using Azure.Core;
using System.Collections.Generic;
namespace ADFJob
{
class Program
{
static async Task Main(string[] args)
{
DotNetEnv.Env.Load();
//var cred = new EnvironmentCredential();
// var ctx = new Azure.Core.TokenRequestContext(new string[] { "https://management.azure.com/.default" });
//var token = await cred.GetTokenAsync(ctx);
var secretClient = new SecretClient(
new Uri(Environment.GetEnvironmentVariable("AZURE_KEYVAULT_URL")),
new DefaultAzureCredential());
var clientId = await secretClient.GetSecretAsync("clientid");
var clientSecret = await secretClient.GetSecretAsync("clientsecret");
var tenantId = await secretClient.GetSecretAsync("tenantid");
//var options = new TokenCredentialOptions();
//options.AuthorityHost = new Uri("https://login.microsoftonline.com/");
var cred = new ClientSecretCredential(tenantId.Value.Value, clientId.Value.Value, clientSecret.Value.Value);
var ctx = new TokenRequestContext(new string[] { "https://management.azure.com/.default" });
var token = await cred.GetTokenAsync(ctx);
var mgmtToken = new TokenCredentials(token.Token);
var client = new DataFactoryManagementClient(mgmtToken);
client.SubscriptionId = "";
var pipelines = await client.Pipelines.GetAsync("adfjob", "adfjob", "pipeline1");
var parameters = new Dictionary<string, object>();
parameters.Add("pipelineSourceTableName", "persons");
parameters.Add("pipelineDestTableName", "persons");
var run = await client.Pipelines.CreateRunAsync("adfjob", "adfjob", "pipeline1", parameters:parameters);
/*
//
var config = new AuthenticationConfig
{
ClientId = "",
ClientSecret = "",
Tenant = ""
};
var app = ConfidentialClientApplicationBuilder.Create(config.ClientId)
.WithAuthority(AzureCloudInstance.AzurePublic, config.Tenant)
.WithClientSecret(config.ClientSecret)
.Build();
AuthenticationResult result = await app.AcquireTokenForClient(new string[] { "https://management.azure.com/.default" }).ExecuteAsync();
Console.WriteLine(result.AccessToken);
var cred = new TokenCredentials(result.AccessToken);
var client = new DataFactoryManagementClient(cred);
client.SubscriptionId = "";
var pipelines = await client.Pipelines.GetAsync("adfjob", "adfjob", "pipeline1");
var run = await client.Pipelines.CreateRunAsync("adfjob", "adfjob", "pipeline1");
*/
}
}
/// <summary>
/// Description of the configuration of an AzureAD public client application (desktop/mobile application). This should
/// match the application registration done in the Azure portal
/// </summary>
public class AuthenticationConfig
{
/// <summary>
/// instance of Azure AD, for example public Azure or a Sovereign cloud (Azure China, Germany, US government, etc ...)
/// </summary>
public string AzureADInstance { get; set; } = "https://login.microsoftonline.com/{0}";
/// <summary>
/// The Tenant is:
/// - either the tenant ID of the Azure AD tenant in which this application is registered (a guid)
/// or a domain name associated with the tenant
/// - or 'organizations' (for a multi-tenant application)
/// </summary>
public string Tenant { get; set; }
/// <summary>
/// Guid used by the application to uniquely identify itself to Azure AD
/// </summary>
public string ClientId { get; set; }
/// <summary>
/// URL of the authority
/// </summary>
public string Authority
{
get
{
return String.Format(CultureInfo.InvariantCulture, AzureADInstance, Tenant);
}
}
#if !VariationWithCertificateCredentials
/// <summary>
/// Client secret (application password)
/// </summary>
/// <remarks>Daemon applications can authenticate with AAD through two mecanisms: ClientSecret
/// (which is a kind of application password: this property)
/// or a certificate previously shared with AzureAD during the application registration
/// (and identified by the CertificateName property belows)
/// <remarks>
public string ClientSecret { get; set; }
#else
/// <summary>
/// Name of a certificate in the user certificate store
/// </summary>
/// <remarks>Daemon applications can authenticate with AAD through two mecanisms: ClientSecret
/// (which is a kind of application password: the property above)
/// or a certificate previously shared with AzureAD during the application registration
/// (and identified by this CertificateName property)
/// <remarks>
public string CertificateName { get; set; }
#endif
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment