Esempio n. 1
0
        static void Main(string[] args)
        {
            // validate parameters
            if (!CheckValidParameters(args))
            {
                return;
            }

            HttpRequestMessage request = null;

            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            try
            {
                // Login as global admin of the Azure AD B2C tenant
                UserMode.LoginAsAdmin();

                // Graph client does not yet support trustFrameworkPolicy, so using HttpClient to make rest calls
                switch (args[0].ToUpper())
                {
                case "LIST":
                    // List all polcies using "GET /trustFrameworkPolicies"
                    request = UserMode.HttpGet(Constants.TrustFrameworkPolicesUri);
                    break;

                case "GET":
                    // Get a specific policy using "GET /trustFrameworkPolicies/{id}"
                    request = UserMode.HttpGetID(Constants.TrustFrameworkPolicyByIDUri, args[1]);
                    break;

                case "CREATE":
                    // Create a policy using "POST /trustFrameworkPolicies" with XML in the body
                    string xml = System.IO.File.ReadAllText(args[1]);
                    request = UserMode.HttpPost(Constants.TrustFrameworkPolicesUri, xml);
                    break;

                case "UPDATE":
                    // Update using "PUT /trustFrameworkPolicies/{id}" with XML in the body
                    xml     = System.IO.File.ReadAllText(args[2]);
                    request = UserMode.HttpPutID(Constants.TrustFrameworkPolicyByIDUri, args[1], xml);
                    break;

                case "DELETE":
                    // Delete using "DELETE /trustFrameworkPolicies/{id}"
                    request = UserMode.HttpDeleteID(Constants.TrustFrameworkPolicyByIDUri, args[1]);
                    break;

                default:
                    return;
                }

                Print(request);

                HttpClient httpClient = new HttpClient();
                Task <HttpResponseMessage> response = httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead);

                Print(response);
            }
            catch (Exception e)
            {
                Print(request);
                Console.WriteLine("\nError {0} {1}", e.Message, e.InnerException != null ? e.InnerException.Message : "");
            }
        }
        /// <summary>
        /// Performs the command.
        /// </summary>
        private static void PerformCommand()
        {
            HttpRequestMessage request = null;

            // ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

            try
            {
                AuthenticationHelper.Init(Inputs.TenantId, Inputs.ClientId);
                if (Inputs.Tokens != null)
                {
                    TokenCacheHelper.CacheContent = Convert.FromBase64String(Inputs.Tokens);
                }

                // Login as global admin of the Azure AD B2C tenant
                UserMode.LoginAsAdmin();

                // Graph client does not yet support trustFrameworkPolicy, so using HttpClient to make rest calls
                switch (Inputs.Command)
                {
                case Commands.LIST:
                {
                    // List all policies using "GET /trustFrameworkPolicies"
                    request = UserMode.HttpGet(Constants.TrustFrameworkPolicesUri);
                    var response = SendRequest(request);
                    PrintListOfPolicies(response);
                    break;
                }

                case Commands.GET:
                {
                    // Get a specific policy using "GET /trustFrameworkPolicies/{id}"
                    request = UserMode.HttpGetID(Constants.TrustFrameworkPolicyByIDUri, Inputs.PolicyId);
                    var response = SendRequest(request);
                    SavePolicyToFile(response);
                    PrintGeneric("Get operation ", response);
                    break;
                }

                case Commands.CREATE:
                {
                    // Create a policy using "POST /trustFrameworkPolicies" with XML in the body
                    string xml = System.IO.File.ReadAllText(Inputs.Path);
                    request = UserMode.HttpPost(Constants.TrustFrameworkPolicesUri, xml);
                    var response = SendRequest(request);
                    PrintGeneric("Create operation", response);
                    break;
                }

                case Commands.UPDATE:
                {
                    // Update using "PUT /trustFrameworkPolicies/{id}" with XML in the body
                    string xml = System.IO.File.ReadAllText(Inputs.Path);
                    request = UserMode.HttpPutID(Constants.TrustFrameworkPolicyByIDUri, Inputs.PolicyId, xml);
                    var response = SendRequest(request);
                    PrintGeneric("Update operation", response);
                    break;
                }

                case Commands.DELETE:
                {
                    // Delete using "DELETE /trustFrameworkPolicies/{id}"
                    request = UserMode.HttpDeleteID(Constants.TrustFrameworkPolicyByIDUri, Inputs.PolicyId);
                    var response = SendRequest(request);
                    PrintGeneric("Delete operation", response);
                    break;
                }

                case Commands.GETTOKENS:
                {
                    PrintTokens();
                    break;
                }

                default:
                    return;
                }
            }
            catch (Exception e)
            {
                Print(request);
                Console.WriteLine("\nError {0} {1}", e.Message, e.InnerException != null ? e.InnerException.Message : "");
            }
        }