Example #1
0
        private async Task <IAccessToken> HandleAuthentication(
            IPowerBIEnvironment environment,
            IPowerBILogger logger,
            IPowerBISettings settings,
            IDictionary <string, string> queryParameters,
            string userName       = null,
            SecureString password = null)
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new NotSupportedException("Authenticator only works on Windows");
            }

            IEnumerable <string> scopes = new[] { $"{environment.AzureADResource}/.default" };

            BuildAuthApplication(environment, queryParameters, logger);
            AuthenticationResult result = null;

            try
            {
                var accounts = await this.AuthApplication.GetAccountsAsync();

                if (accounts != null && accounts.Any())
                {
                    // This indicates there's token in cache
                    result = await this.AuthApplication.AcquireTokenSilent(scopes, accounts.First()).ExecuteAsync();
                }
                else
                {
                    // auth application is auto cleared when there's no account
                    BuildAuthApplication(environment, queryParameters, logger);
                    if (!string.IsNullOrEmpty(userName) && password != null && password.Length > 0)
                    {
                        result = await this.AuthApplication.AcquireTokenByUsernamePassword(scopes, userName, password).ExecuteAsync();
                    }
                    else
                    {
                        result = await this.AuthApplication.AcquireTokenInteractive(scopes).ExecuteAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new AuthenticationException($"Error Acquiring Token:{System.Environment.NewLine}{ex.Message}");
            }

            if (result != null)
            {
                return(result.ToIAccessToken());
                // Use the token
            }
            else
            {
                throw new AuthenticationException("Failed to acquire token");
            }
        }
Example #2
0
        public async Task <IAccessToken> Authenticate(IPowerBIEnvironment environment, IPowerBILogger logger, IPowerBISettings settings, IDictionary <string, string> queryParameters = null)
        {
            IEnumerable <string> scopes = new[] { $"{environment.AzureADResource}/.default" };

            if (this.AuthApplication == null)
            {
                this.AuthApplication = PublicClientApplicationBuilder
                                       .Create(environment.AzureADClientId)
                                       .WithAuthority(environment.AzureADAuthority)
                                       .WithLogging((level, message, containsPii) => LoggingUtils.LogMsal(level, message, containsPii, logger))
                                       .WithRedirectUri(environment.AzureADRedirectAddress)
                                       .Build();
            }

            AuthenticationResult result = null;
            var accounts = await AuthApplication.GetAccountsAsync();

            if (accounts != null && accounts.Any())
            {
                try
                {
                    result = await AuthApplication.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();

                    return(result.ToIAccessToken());
                }
                catch (MsalUiRequiredException)
                {
                    // ignore and fall through to aquire through device code
                }
            }

            DeviceCodeResult deviceCodeResult = null;

            result = await AuthApplication.AcquireTokenWithDeviceCode(scopes, r => { Console.WriteLine(r.Message); deviceCodeResult = r; return(Task.FromResult(0)); }).ExecuteAsync();

            return(result.ToIAccessToken());
        }
Example #3
0
        public async Task <IAccessToken> Authenticate(string clientId, string thumbprint, IPowerBIEnvironment environment, IPowerBILogger logger, IPowerBISettings settings)
        {
            var certificate             = FindCertificate(thumbprint);
            IEnumerable <string> scopes = new[] { $"{environment.AzureADResource}/.default" };

            BuildAuthApplicationCert(environment, clientId, certificate, logger);
            AuthenticationResult result = null;

            try
            {
                var accounts = await this.AuthApplicationCert.GetAccountsAsync();

                if (accounts != null && accounts.Any())
                {
                    // This indicates there's token in cache
                    result = await this.AuthApplicationCert.AcquireTokenSilent(scopes, accounts.FirstOrDefault()).ExecuteAsync();
                }
                else
                {
                    BuildAuthApplicationCert(environment, clientId, certificate, logger);
                    result = await this.AuthApplicationCert.AcquireTokenForClient(scopes).ExecuteAsync();
                }
            }
            catch (Exception ex)
            {
                throw new AuthenticationException($"Error Acquiring Token:{System.Environment.NewLine}{ex}");
            }

            if (result != null)
            {
                return(result.ToIAccessToken());
                // Use the token
            }
            else
            {
                throw new AuthenticationException("Failed to acquire token");
            }
        }