Esempio n. 1
0
        /// <inheritdoc />
        public async Task <AuthenticationResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            if (_authParameters.AuthorizationType != AuthorizationType.AuthCode)
            {
                var cacheResult = await TryReadCacheAsync(cancellationToken).ConfigureAwait(false);

                if (cacheResult.IsCacheReadSuccessful)
                {
                    return(AuthenticationResult.Create(cacheResult.TokenResponse, cacheResult.Account));
                }
            }

            TokenResponse tokenResponse;

            switch (_authParameters.AuthorizationType)
            {
            case AuthorizationType.AuthCode:
                tokenResponse = await AuthCodeExchangeAsync(cancellationToken).ConfigureAwait(false);

                break;

            case AuthorizationType.UsernamePassword:
                tokenResponse = await UsernamePasswordExchangeAsync(cancellationToken).ConfigureAwait(false);

                break;

            case AuthorizationType.WindowsIntegratedAuth:
                tokenResponse = await WindowsIntegratedAuthExchangeAsync(cancellationToken).ConfigureAwait(false);

                break;

            case AuthorizationType.Certificate:
                tokenResponse = await CertificateExchangeAsync(cancellationToken).ConfigureAwait(false);

                break;

            case AuthorizationType.None:
                throw new InvalidOperationException("msal background request called with None type");

            default:
                throw new InvalidOperationException("unknown or unsupported auth type");
            }

            var account = await _cacheManager.CacheTokenResponseAsync(tokenResponse).ConfigureAwait(false);

            return(AuthenticationResult.Create(tokenResponse, account));
        }