Ejemplo n.º 1
0
        public static string GetToken()
        {
            return(AuthUtil.GetTokenFromConsole());

https:      //yourazurecoach.com/2020/08/13/managed-identity-simplified-with-the-new-azure-net-sdks/
            var options = new DefaultAzureCredentialOptions
            {
                ExcludeEnvironmentCredential        = false,
                ExcludeManagedIdentityCredential    = false,
                ExcludeSharedTokenCacheCredential   = false,
                ExcludeVisualStudioCredential       = true,
                ExcludeVisualStudioCodeCredential   = true,
                ExcludeAzureCliCredential           = false,
                ExcludeInteractiveBrowserCredential = false
            };
            var credential = new DefaultAzureCredential(options);
            //var credential = new ChainedTokenCredential(new AzureCliCredential(), new ManagedIdentityCredential());
            var token = credential.GetToken(
                new Azure.Core.TokenRequestContext(
                    new[] { "https://graph.microsoft.com/.default" }));

            var accessToken = token.Token;

            //var graphServiceClient = new GraphServiceClient(
            //    new DelegateAuthenticationProvider((requestMessage) =>
            //    {
            //        requestMessage
            //            .Headers
            //            .Authorization = new AuthenticationHeaderValue("bearer", accessToken);

            //        return Task.CompletedTask;
            //    }));

            return(accessToken);
        }
Ejemplo n.º 2
0
 public void Authenticate(string token = null)
 {
     if (_hasAuthenticated)
     {
         return;
     }
     Consoler.TitleStart("Authenticating...");
     Token             = !string.IsNullOrEmpty(token) ? token : AuthUtil.GetTokenFromConsole();
     Client            = new ApiClient(AzureEnvVars.TenantId, AzureEnvVars.SubscriptionId, Token);
     _hasAuthenticated = true;
     Consoler.TitleEnd("Authenticating...");
 }