Skip to content

Instantly share code, notes, and snippets.

@kimsk
Last active April 22, 2019 19:36
Show Gist options
  • Save kimsk/3dd4375e9f98b7d57a27bed0f55916e5 to your computer and use it in GitHub Desktop.
Save kimsk/3dd4375e9f98b7d57a27bed0f55916e5 to your computer and use it in GitHub Desktop.
Using MSI locally with Azure Management Libraries for .NET (Fluent)
// https://docs.microsoft.com/en-us/dotnet/azure/dotnet-sdk-azure-concepts?view=azure-dotnet
// https://github.com/Azure/azure-libraries-for-net/issues/585
// https://github.com/Azure/azure-libraries-for-net#ready-to-run-code-samples-for-databases
open Microsoft.Azure.Services.AppAuthentication
open Microsoft.Azure.Management.ResourceManager.Fluent.Authentication
open Microsoft.Azure.Management.ResourceManager.Fluent
open Microsoft.Azure.Management.Fluent
open Microsoft.Azure.Management.ResourceManager.Fluent.Core
open Microsoft.Rest
let azureServiceTokenProvider = AzureServiceTokenProvider()
let graphToken =
azureServiceTokenProvider.GetAccessTokenAsync("https://graph.windows.net/")
|> Async.AwaitTask
|> Async.RunSynchronously
let rmToken =
azureServiceTokenProvider.GetAccessTokenAsync("https://management.azure.com")
|> Async.AwaitTask
|> Async.RunSynchronously
let tenantId = azureServiceTokenProvider.PrincipalUsed.TenantId
printfn "%s %s %s" graphToken rmToken tenantId
let credentials =
AzureCredentials(
TokenCredentials(rmToken),
TokenCredentials(graphToken),
tenantId,
AzureEnvironment.AzureGlobalCloud
)
let client =
RestClient
.Configure()
.WithEnvironment(AzureEnvironment.AzureGlobalCloud)
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.WithCredentials(credentials)
.Build()
let authenticated = Azure.Authenticate(client, tenantId)
authenticated.Subscriptions.List()
|> Seq.iter (fun s -> printfn "%s" s.DisplayName)
let azure = authenticated.WithDefaultSubscription()
azure.AppServices.WebApps.List()
|> Seq.iter (fun x -> printfn "%s" x.Name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment