public async Task <IAdalToken> AcquireTokenWithWindowsIntegratedAuth(CancellationToken cancellationToken)
        {
            var authenticationContext = new AuthenticationContext(authority, tokenCache);

            try
            {
                string upn = WindowsIntegratedAuthUtils.GetUserPrincipalName();
                if (upn == null)
                {
                    return(null);
                }

                var result = await authenticationContext.AcquireTokenAsync(resource, clientId, new UserCredential(upn));

                cancellationToken.ThrowIfCancellationRequested();

                return(new AdalToken(result));
            }
            catch (AdalServiceException e)
            {
                if (e.ErrorCode == AdalError.AuthenticationCanceled)
                {
                    return(null);
                }

                throw;
            }
        }
Example #2
0
        public async Task <IMsalToken> AcquireTokenWithWindowsIntegratedAuth(CancellationToken cancellationToken)
        {
            var publicClient = await GetPCAAsync().ConfigureAwait(false);

            try
            {
                string upn = WindowsIntegratedAuthUtils.GetUserPrincipalName();
                if (upn == null)
                {
                    return(null);
                }

                var builder = publicClient.AcquireTokenByIntegratedWindowsAuth(new string[] { resource });
                builder.WithUsername(upn);
                var result = await builder.ExecuteAsync(cancellationToken);

                return(new MsalToken(result));
            }
            catch (MsalServiceException e)
            {
                if (e.ErrorCode.Contains(MsalError.AuthenticationCanceledError))
                {
                    return(null);
                }

                throw;
            }
            finally
            {
                var helper = await GetMsalCacheHelperAsync();

                helper?.UnregisterCache(publicClient.UserTokenCache);
            }
        }
Example #3
0
 public bool ShouldRun(bool isRetry, bool isNonInteractive, bool canShowDialog)
 {
     return(WindowsIntegratedAuthUtils.SupportsWindowsIntegratedAuth() && EnvUtil.WindowsIntegratedAuthenticationEnabled());
 }
Example #4
0
        public async Task <BearerTokenResult> GetAsync(Uri uri, bool isRetry, bool isNonInteractive, bool canShowDialog, CancellationToken cancellationToken)
        {
            var authority = await authUtil.GetAadAuthorityUriAsync(uri, cancellationToken);

            logger.Verbose(string.Format(Resources.AdalUsingAuthority, authority));

            var adalTokenProvider = adalTokenProviderFactory.Get(authority.ToString());

            cancellationToken.ThrowIfCancellationRequested();

            IAdalToken adalToken;

            // Try to acquire token silently
            if (!isRetry)
            {
                adalToken = await adalTokenProvider.AcquireTokenSilentlyAsync(cancellationToken);

                if (adalToken?.AccessToken != null)
                {
                    logger.Verbose(Resources.AdalAcquireTokenSilentSuccess);
                    return(new BearerTokenResult(adalToken.AccessToken, obtainedInteractively: false));
                }
                else
                {
                    logger.Verbose(Resources.AdalAcquireTokenSilentFailed);
                }
            }

            // Try Windows Integrated Auth if supported
            if (WindowsIntegratedAuthUtils.SupportsWindowsIntegratedAuth())
            {
                adalToken = await adalTokenProvider.AcquireTokenWithWindowsIntegratedAuth(cancellationToken);

                if (adalToken?.AccessToken != null)
                {
                    logger.Verbose(Resources.AdalAcquireTokenWIASuccess);
                    return(new BearerTokenResult(adalToken.AccessToken, obtainedInteractively: false));
                }
                else
                {
                    logger.Verbose(Resources.AdalAcquireTokenWIAFailed);
                }
            }

            // Interactive flows if allowed
            if (!isNonInteractive)
            {
#if NETFRAMEWORK
                if (canShowDialog && RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    // Try UI prompt
                    adalToken = await adalTokenProvider.AcquireTokenWithUI(cancellationToken);

                    if (adalToken?.AccessToken != null)
                    {
                        return(new BearerTokenResult(adalToken.AccessToken, obtainedInteractively: true));
                    }
                }
#endif

                // Try device flow
                adalToken = await adalTokenProvider.AcquireTokenWithDeviceFlowAsync(
                    (DeviceCodeResult deviceCodeResult) =>
                {
                    logger.Minimal(string.Format(Resources.AdalDeviceFlowRequestedResource, uri.ToString()));
                    logger.Minimal(string.Format(Resources.AdalDeviceFlowMessage, deviceCodeResult.VerificationUrl, deviceCodeResult.UserCode));

                    return(Task.CompletedTask);
                },
                    cancellationToken);

                if (adalToken?.AccessToken != null)
                {
                    logger.Verbose(Resources.AdalAcquireTokenDeviceFlowSuccess);
                    return(new BearerTokenResult(adalToken.AccessToken, obtainedInteractively: true));
                }
                else
                {
                    logger.Verbose(Resources.AdalAcquireTokenDeviceFlowFailed);
                }
            }
            else if (isRetry)
            {
                logger.Warning(Resources.CannotRetryWithNonInteractiveFlag);
            }

            logger.Verbose(string.Format(Resources.AdalTokenNotFound, uri.ToString()));
            return(null);
        }