public async Task AuthenticateUsernamePasswordLiveAsync()
        {
            var username = Environment.GetEnvironmentVariable("IDENTITYTEST_USERNAMEPASSWORDCREDENTIAL_USERNAME");

            var password = Environment.GetEnvironmentVariable("IDENTITYTEST_USERNAMEPASSWORDCREDENTIAL_PASSWORD");

            var tenantId = Environment.GetEnvironmentVariable("IDENTITYTEST_USERNAMEPASSWORDCREDENTIAL_TENANTID");

            var cred = new UsernamePasswordCredential(username, password, tenantId, ClientId);

            AccessToken token = await cred.GetTokenAsync(new TokenRequestContext(new string[] { "https://vault.azure.net/.default" }));

            Assert.IsNotNull(token.Token);
        }
        public override Task <IAccessToken> Authenticate(AuthenticationParameters parameters, CancellationToken cancellationToken)
        {
            var upParameters       = parameters as UsernamePasswordParameters;
            var onPremise          = upParameters.Environment.OnPremise;
            var tenantId           = onPremise ? AdfsTenant : upParameters.TenantId; //Is user name + password valid in Adfs env?
            var tokenCacheProvider = upParameters.TokenCacheProvider;
            var resource           = upParameters.Environment.GetEndpoint(upParameters.ResourceId) ?? upParameters.ResourceId;
            var scopes             = AuthenticationHelpers.GetScope(onPremise, resource);
            var clientId           = AuthenticationHelpers.PowerShellClientId;
            var authority          = upParameters.Environment.ActiveDirectoryAuthority;

            var requestContext = new TokenRequestContext(scopes);
            UsernamePasswordCredential passwordCredential;

            AzureSession.Instance.TryGetComponent(nameof(PowerShellTokenCache), out PowerShellTokenCache tokenCache);

            var credentialOptions = new UsernamePasswordCredentialOptions()
            {
                AuthorityHost = new Uri(authority),
                TokenCache    = tokenCache.TokenCache
            };

            if (upParameters.Password != null)
            {
                passwordCredential = new UsernamePasswordCredential(upParameters.UserId, upParameters.Password.ConvertToString(), tenantId, clientId, credentialOptions);
                var authTask = passwordCredential.AuthenticateAsync(requestContext, cancellationToken);
                return(MsalAccessToken.GetAccessTokenAsync(
                           authTask,
                           () => passwordCredential.GetTokenAsync(requestContext, cancellationToken),
                           cancellationToken));
            }
            else
            {
                throw new InvalidOperationException(Resources.MissingPasswordAndNoCache);
            }
        }