/// <summary>
        /// Creates and returns a ManagementService object. This is the only 'interface' used by other classes.
        /// </summary>
        public static ManagementService CreateManagementServiceClient()
        {
            string managementServiceHead = "v2/mgmt/service/";
            string managementServiceEndpoint  = string.Format("https://{0}.{1}/{2}", serviceNamespace, acsHostName, managementServiceHead);
            ManagementService managementService     = new ManagementService(new Uri(managementServiceEndpoint));

            managementService.SendingRequest += GetTokenWithWritePermission;

            return managementService;
        }
        /// <summary>
        /// Add an Identity Provider
        /// </summary>
        private static Issuer CreateIdpManually(DateTime startDate, DateTime endDate, ManagementService svc0, string idpName, string idpDisplayName, string idpAddress, string idpKeyDisplayName)
        {
            var issuer = new Issuer
            {
                Name = idpName
            };

            // Check the Issuer does not exist previouly (if it exists, delete it)
            var oldIssuer = svc0.Issuers.Where(ip => ip.Name == issuer.Name).FirstOrDefault();
            if (oldIssuer != null)
            {
                svc0.DeleteObject(oldIssuer);
                svc0.SaveChanges();
            }

            // Add Issuer
            svc0.AddToIssuers(issuer);
            svc0.SaveChanges(SaveChangesOptions.Batch);
            Console.WriteLine("Info: Issuer created: {0}", idpName);

            var idp = new IdentityProvider
            {
                DisplayName = idpDisplayName,
                LoginLinkName = idpDisplayName,
                WebSSOProtocolType = "WsFederation",
                IssuerId = issuer.Id
            };

            // Check the IP does not exist previouly (if it exists, delete it)
            var oldIdentityProvider = svc0.IdentityProviders.Where(ip => ip.DisplayName == idp.DisplayName).FirstOrDefault();
            if (oldIdentityProvider != null)
            {
                svc0.DeleteObject(oldIdentityProvider);
                svc0.SaveChanges();
            }

            // Add the new IP to ACS
            svc0.AddObject("IdentityProviders", idp);

            // Console.WriteLine("Info: Identity Provider created: {0}", idp.Name);
            Console.WriteLine("Info: Identity Provider created: {0}", idp.DisplayName);

            // Identity provider public key to verify the signature
            var cert = File.ReadAllBytes(@"Resources\SelfSTS.cer");
            var key = new IdentityProviderKey
            {
                IdentityProvider = idp,
                DisplayName = idpKeyDisplayName,
                EndDate = endDate,
                StartDate = startDate,
                Type = "X509Certificate",
                Usage = "Signing",
                Value = cert
            };

            svc0.AddRelatedObject(idp, "IdentityProviderKeys", key);
            svc0.SaveChanges(SaveChangesOptions.Batch);

            Console.WriteLine("Info: Identity Provider Key added: {0}", idpKeyDisplayName);

            // WS-Federation sign-in URL
            var idpaSignIn = new IdentityProviderAddress
            {
                IdentityProviderId = idp.Id,
                EndpointType = "SignIn",
                Address = idpAddress
            };

            svc0.AddRelatedObject(idp, "IdentityProviderAddresses", idpaSignIn);
            svc0.SaveChanges(SaveChangesOptions.Batch);

            Console.WriteLine("Info: Identity Provider Address added: {0}", idpAddress);

            string labRelyingPartyName = "WebSiteAdvancedACS";

            // Relying Party related to the Identity Provider
            foreach (var existingRelyingParty in svc0.RelyingParties)
            {
                var rpid = new RelyingPartyIdentityProvider
                {
                    IdentityProviderId = idp.Id,
                    RelyingPartyId = existingRelyingParty.Id
                };
                existingRelyingParty.RelyingPartyIdentityProviders.Add(rpid);
                idp.RelyingPartyIdentityProviders.Add(rpid);
                svc0.AddToRelyingPartyIdentityProviders(rpid);
            }

            svc0.SaveChanges(SaveChangesOptions.Batch);

            Console.WriteLine("Info: Relying Party added to Identity Provider: {0}", labRelyingPartyName);

            return issuer;
        }