예제 #1
0
        internal static async Task <GetKeysResponse> GetSubscriptionKeys(
            HttpClient client,
            string azureSubscriptionId,
            string resourceGroupName,
            string serviceName,
            string subscriptionId,
            string token,
            ILogger log)
        {
            // Reference: https://docs.microsoft.com/en-us/rest/api/apimanagement/2019-01-01/subscription/get

            // Format the request URI
            var getSubscriptionUri =
                $"https://management.azure.com/subscriptions/{azureSubscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ApiManagement/service/{serviceName}/subscriptions/{subscriptionId}?api-version=2019-01-01";

            // Clear the request headers, set the content type
            // and add the bearer token.
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Remove("Authorization");
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);

            // Make the request to retrieve the subscription information
            var response = await client.GetAsync(getSubscriptionUri);

            var result = await response.Content.ReadAsStringAsync();

            // Format the response and return
            var json            = JObject.Parse(result);
            var getKeysResponse = new GetKeysResponse
            {
                PrimaryKey   = json["properties"]["primaryKey"].ToString(),
                SecondaryKey = json["properties"]["secondaryKey"].ToString()
            };

            return(getKeysResponse);
        }
예제 #2
0
        static int Main(string[] args)
        {
            // Please set keyIds to keys you have already created.
            String keyId1 = null;
            String keyId2 = null;
            String keyId3 = null;

            if (keyId1 == null || keyId2 == null || keyId3 == null)
            {
                Console.WriteLine("Please set the keyIds to keys you have already created.");
                WaitForInput();
                Environment.Exit(1);
            }

            // Get the user's home path and password persistor from the environment.
            String homePath = Environment.GetEnvironmentVariable("USERPROFILE");

            String persistorPassword = Environment.GetEnvironmentVariable("IONIC_PERSISTOR_PASSWORD");

            if (persistorPassword == null || persistorPassword.Length == 0)
            {
                Console.WriteLine("Please provide the persistor password as env variable: IONIC_PERSISTOR_PASSWORD");
                WaitForInput();
                Environment.Exit(1);
            }

            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

            // Create a password persistor for agent initialization.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = homePath + "\\.ionicsecurity\\profiles.pw";
                persistor.Password = persistorPassword;

                agent.SetMetadata(Agent.MetaApplicationName, "GetMultipleKeys Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Create a key request for multiple keys.
            GetKeysRequest keyRequest = new GetKeysRequest();

            keyRequest.KeyIds.Add(keyId1);
            keyRequest.KeyIds.Add(keyId2);
            keyRequest.KeyIds.Add(keyId3);

            // Fetch multiple keys from the agent.
            GetKeysResponse getKeysResponse = null;

            try
            {
                getKeysResponse = agent.GetKeys(keyRequest);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching keys: {1}", sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Pull the keys out of the response.
            List <GetKeysResponse.Key> keys = getKeysResponse.Keys;

            if (keys.Count == 0)
            {
                Console.WriteLine("No keys returned for external IDs. (Key does not exist or access was denied.)");
                WaitForInput();
                Environment.Exit(1);
            }

            foreach (GetKeysResponse.Key key in keys)
            {
                Console.WriteLine("-----");
                Console.WriteLine("Key ID             : " + key.Id);
                Console.WriteLine("Key Bytes          : " + BitConverter.ToString(key.KeyBytes).Replace("-", String.Empty));
                Console.WriteLine("Fixed Attributes   : " + JsonDump(key.Attributes));
                Console.WriteLine("Mutable Attributes : " + JsonDump(key.MutableAttributes));
            }

            WaitForInput();
            return(0);
        }
예제 #3
0
        static int Main(string[] args)
        {
            // Please set keyId to a key you have already created.
            String keyId = null;

            if (keyId == null)
            {
                Console.WriteLine("Please set the keyId to a key you have already created.");
                WaitForInput();
                Environment.Exit(1);
            }

            // Get the user's home path and password persistor from the environment.
            String homePath = Environment.GetEnvironmentVariable("USERPROFILE");

            String persistorPassword = Environment.GetEnvironmentVariable("IONIC_PERSISTOR_PASSWORD");

            if (persistorPassword == null || persistorPassword.Length == 0)
            {
                Console.WriteLine("Please provide the persistor password as env variable: IONIC_PERSISTOR_PASSWORD");
                WaitForInput();
                Environment.Exit(1);
            }

            // Create an agent object to talk to Ionic.
            Agent agent = new Agent();

            // Create a password persistor for agent initialization.
            try
            {
                DeviceProfilePersistorPassword persistor = new DeviceProfilePersistorPassword();
                persistor.FilePath = homePath + "\\.ionicsecurity\\profiles.pw";
                persistor.Password = persistorPassword;

                agent.SetMetadata(Agent.MetaApplicationName, "UpdateKey Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Fetch the key from the agent.
            GetKeysResponse fetchedResponse = null;

            try
            {
                fetchedResponse = agent.GetKey(keyId);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error fetching key {0}: {1}", keyId, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Pull the key out of the response.
            GetKeysResponse.Key fetchedKey = fetchedResponse.Keys[0];

            // Define mutable key attributes
            AttributesDictionary newMutableKeyAttrs = new AttributesDictionary();

            newMutableKeyAttrs.Add("classification", new List <string> {
                "Highly Restricted"
            });

            // Create the update key request.
            bool forceUpdate = false;
            UpdateKeysRequest updateKeysRequest = new UpdateKeysRequest();

            UpdateKeysRequest.Key updateKey = new UpdateKeysRequest.Key(fetchedKey, forceUpdate);
            updateKey.MutableAttributes = newMutableKeyAttrs;
            updateKeysRequest.addKey(updateKey);

            // Update the key attributes on the agent.
            UpdateKeysResponse.Key key = null;
            try
            {
                key = agent.UpdateKeys(updateKeysRequest).Keys[0];
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Error updating key {0}: {1}", keyId, sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }


            Console.WriteLine("Key ID             : " + key.Id);
            Console.WriteLine("Key Bytes          : " + BitConverter.ToString(key.KeyBytes).Replace("-", String.Empty));
            Console.WriteLine("Fixed Attributes   : " + JsonDump(key.Attributes));
            Console.WriteLine("Mutable Attributes : " + JsonDump(key.MutableAttributes));

            WaitForInput();
            return(0);
        }