Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active Jul 5, 2022
Embed
What would you like to do?
Convert DefaultAzureCredential to AzureCredentials.
using System;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Identity;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
// DefaultAzureCredentialで認証をとって Token を取得する
var credential = new DefaultAzureCredential(new DefaultAzureCredentialOptions { /* VSを除外したりとオプション指定 */ });
var defaultToken = await credential.GetTokenAsync(new TokenRequestContext(new[] { "https://management.azure.com/.default" }));
var defaultTokenCredential = new Microsoft.Rest.TokenCredentials(defaultToken.Token);
// Token をさすことで認証が取れる
var cred = new AzureCredentials(defaultTokenCredential,defaultTokenCredential, null, AzureEnvironment.AzureGlobalCloud);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment