Last active
April 22, 2019 19:36
-
-
Save kimsk/3dd4375e9f98b7d57a27bed0f55916e5 to your computer and use it in GitHub Desktop.
Using MSI locally with Azure Management Libraries for .NET (Fluent)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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