/// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            Scheduler.RunTask(async() =>
            {
                MgmtAccount account = new MgmtAccount();
                string applicationId;

                if (!string.IsNullOrEmpty(CertificateThumbprint))
                {
                    account.SetProperty(MgmtAccountPropertyType.CertificateThumbprint, CertificateThumbprint);
                }

                if (ParameterSetName.Equals(RefreshTokenParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (Credential != null)
                    {
                        account.ObjectId = Credential.UserName;
                        account.SetProperty(MgmtAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString());
                        applicationId = Credential.UserName;
                    }
                    else
                    {
                        applicationId = ApplicationId;
                    }
                }
                else if (ParameterSetName.Equals(ServicePrincipalCertificateParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    account.SetProperty(MgmtAccountPropertyType.ApplicationId, ApplicationId);
                    account.Type  = AccountType.Certificate;
                    applicationId = ApplicationId;
                }
                else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    account.ObjectId = Credential.UserName;
                    account.SetProperty(MgmtAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString());
                    account.Type  = AccountType.ServicePrincipal;
                    applicationId = ApplicationId;
                }
                else
                {
                    account.Type  = AccountType.User;
                    applicationId = ApplicationId;
                }

                if (UseAuthorizationCode.IsPresent)
                {
                    account.SetProperty(MgmtAccountPropertyType.UseAuthCode, "true");
                }

                if (UseDeviceAuthentication.IsPresent)
                {
                    account.SetProperty(MgmtAccountPropertyType.UseDeviceAuth, "true");
                }

                if (!string.IsNullOrEmpty(RefreshToken))
                {
                    account.SetProperty(MgmtAccountPropertyType.RefreshToken, RefreshToken);
                }

                account.SetProperty(MgmtAccountPropertyType.ApplicationId, applicationId);
                account.Tenant = string.IsNullOrEmpty(Tenant) ? "organizations" : Tenant;

                AuthenticationResult authResult = await MgmtSession.Instance.AuthenticationFactory.AuthenticateAsync(
                    account,
                    MgmtEnvironment.PublicEnvironments[Environment],
                    Scopes,
                    Message,
                    CancellationToken).ConfigureAwait(false);

                AuthResult result = new AuthResult(
                    authResult.AccessToken,
                    authResult.IsExtendedLifeTimeToken,
                    authResult.UniqueId,
                    authResult.ExpiresOn,
                    authResult.ExtendedExpiresOn,
                    authResult.TenantId,
                    authResult.Account,
                    authResult.IdToken,
                    authResult.Scopes,
                    authResult.CorrelationId);

                byte[] cacheData = null;

                if (MgmtSession.Instance.TryGetComponent(ComponentKey.TokenCache, out IMgmtTokenCache tokenCache))
                {
                    cacheData = tokenCache.GetCacheData();

                    if (cacheData?.Length > 0)
                    {
                        IEnumerable <string> knownPropertyNames = new[] { "AccessToken", "RefreshToken", "IdToken", "Account", "AppMetadata" };

                        JObject root = JObject.Parse(Encoding.UTF8.GetString(cacheData, 0, cacheData.Length));

                        IDictionary <string, JToken> known = (root as IDictionary <string, JToken>)
                                                             .Where(kvp => knownPropertyNames.Any(p => string.Equals(kvp.Key, p, StringComparison.OrdinalIgnoreCase)))
                                                             .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                        IDictionary <string, TokenCacheItem> tokens = new Dictionary <string, TokenCacheItem>();

                        if (known.ContainsKey("RefreshToken"))
                        {
                            foreach (JToken token in root["RefreshToken"].Values())
                            {
                                if (token is JObject j)
                                {
                                    TokenCacheItem item = new TokenCacheItem
                                    {
                                        ClientId       = ExtractExistingOrEmptyString(j, "client_id"),
                                        CredentialType = ExtractExistingOrEmptyString(j, "credential_type"),
                                        Environment    = ExtractExistingOrEmptyString(j, "environment"),
                                        HomeAccountId  = ExtractExistingOrEmptyString(j, "home_account_id"),
                                        RawClientInfo  = ExtractExistingOrEmptyString(j, "client_info"),
                                        Secret         = ExtractExistingOrEmptyString(j, "secret")
                                    };

                                    tokens.Add($"{item.HomeAccountId}-{item.Environment}-RefreshToken-{item.ClientId}--", item);
                                }
                            }
                        }

                        if (authResult.Account != null)
                        {
                            string key = tokenCache.GetCacheKey(authResult);

                            if (tokens.ContainsKey(key))
                            {
                                result.RefreshToken = tokens[key].Secret;
                            }
                        }
                    }
                    else
                    {
                        WriteDebug("There was not any data in the token cache, so a refresh token could not be retrieved.");
                    }
                }
                else
                {
                    WriteDebug("Unable to find a registered token cache.");
                }

                WriteObject(result);
            });
        }
        /// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            Scheduler.RunTask(async() =>
            {
                MgmtAccount account         = new MgmtAccount();
                MgmtEnvironment environment = MgmtEnvironment.PublicEnvironments[Environment];

                if (!string.IsNullOrEmpty(CertificateThumbprint))
                {
                    account.SetProperty(MgmtAccountPropertyType.CertificateThumbprint, CertificateThumbprint);
                }

                if (!string.IsNullOrEmpty(RefreshToken))
                {
                    account.SetProperty(MgmtAccountPropertyType.RefreshToken, RefreshToken);
                }

                account.SetProperty(MgmtAccountPropertyType.ApplicationId, string.IsNullOrEmpty(ApplicationId) ? PowerShellApplicationId : ApplicationId);

                if (ParameterSetName.Equals(AccessTokenParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    account.SetProperty(MgmtAccountPropertyType.AccessToken, AccessToken);
                    account.Type = AccountType.AccessToken;
                }
                else if (ParameterSetName.Equals(RefreshTokenParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    if (Credential != null)
                    {
                        account.ObjectId = Credential.UserName;
                        account.SetProperty(MgmtAccountPropertyType.ApplicationId, Credential.UserName);
                        account.SetProperty(MgmtAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString());
                    }
                }
                else if (ParameterSetName.Equals(ServicePrincipalCertificateParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    account.SetProperty(MgmtAccountPropertyType.ApplicationId, ApplicationId);
                    account.Type = AccountType.Certificate;
                }
                else if (ParameterSetName.Equals(ServicePrincipalParameterSet, StringComparison.InvariantCultureIgnoreCase))
                {
                    account.ObjectId = Credential.UserName;
                    account.Type     = AccountType.ServicePrincipal;

                    account.SetProperty(MgmtAccountPropertyType.ApplicationId, Credential.UserName);
                    account.SetProperty(MgmtAccountPropertyType.ServicePrincipalSecret, Credential.Password.ConvertToString());
                }
                else
                {
                    account.Type = AccountType.User;
                }

                if (UseDeviceAuthentication.IsPresent)
                {
                    account.SetProperty("UseDeviceAuth", "true");
                }

                account.SetProperty(MgmtAccountPropertyType.Scope, $"{environment.GraphEndpoint}/.default");

                account.Tenant = string.IsNullOrEmpty(Tenant) ? "organizations" : Tenant;

                await MgmtSession.Instance.AuthenticationFactory.AuthenticateAsync(
                    account,
                    environment,
                    new[] { account.GetProperty(MgmtAccountPropertyType.Scope) },
                    Message,
                    CancellationToken).ConfigureAwait(false);

                MgmtSession.Instance.Context = new MgmtContext
                {
                    Account     = account,
                    Environment = environment
                };

                WriteObject(MgmtSession.Instance.Context);
            });
        }