public void TestDefaultPersistor()
        {
            DeviceProfilePersistorPlaintext plainpersistor = new DeviceProfilePersistorPlaintext();

            plainpersistor.FilePath = "C:\\Users\\jimmy.inIS\\Desktop\\plaintext.txt";

            Console.WriteLine(plainpersistor.FilePath);

            DeviceProfilePersistorDefault persistor = new DeviceProfilePersistorDefault();

            //load profiles
            List <DeviceProfile> profiles = null;
            String activeDeviceId         = null;

            plainpersistor.LoadAllProfiles(ref profiles, ref activeDeviceId);

            Console.WriteLine(activeDeviceId);

            Agent agent = new Agent();

            agent.Initialize(plainpersistor);

            CreateKeysRequest.Key requestKey = new CreateKeysRequest.Key("refid_test");
            requestKey.Attributes.Add("classifications", new List <string>());
            requestKey.Attributes["classifications"].Add("c1");

            CreateKeysRequest request = new CreateKeysRequest();

            request.Keys.Add(requestKey);

            CreateKeysResponse response = agent.CreateKeys(request);

            /*
             * persistor.SaveAllProfiles(profiles, activeDeviceId);
             *
             * List<DeviceProfile> profiles2 = null;
             * String activeDeviceId2 = null;
             * persistor.LoadAllProfiles(ref profiles2, ref activeDeviceId2);
             *
             * Console.WriteLine("----------------");
             * Console.WriteLine(activeDeviceId);
             *
             * Assert.AreEqual(profiles.Count, profiles2.Count);
             * Assert.AreEqual(activeDeviceId, activeDeviceId2);
             */
        }
示例#2
0
        static int Main(string[] args)
        {
            // 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, "CreateMultipleKeys Sample");
                agent.Initialize(persistor);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Agent initialization error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            String keyRef   = "sample";
            int    keyCount = 5;

            // Create multiple keys request.
            CreateKeysRequest.Key keysToCreate      = new CreateKeysRequest.Key(keyRef, keyCount);
            CreateKeysRequest     createKeysRequest = new CreateKeysRequest();

            createKeysRequest.Keys.Add(keysToCreate);

            // Invoke the agent to create the keys.
            CreateKeysResponse createKeysResponse = null;

            try
            {
                createKeysResponse = agent.CreateKeys(createKeysRequest);
            }
            catch (SdkException sdkExp)
            {
                Console.WriteLine("Create multiple keys error: " + sdkExp.Message);
                WaitForInput();
                Environment.Exit(1);
            }

            // Pull the keys out of the response.
            List <CreateKeysResponse.Key> keys = createKeysResponse.Keys;

            foreach (CreateKeysResponse.Key key in keys)
            {
                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)
        {
            // Initialize the Ionic agent
            agent = new Agent();
            agent.Initialize();

            // Request keys
            // Forming the key request object
            CreateKeysRequest request = new CreateKeysRequest();
            // Here update request with the list of what it should create.
            AttributesDictionary dirAttributes   = new AttributesDictionary();
            List <string>        listClassValues = new List <string>(1);

            listClassValues.Add("restricted");
            dirAttributes.Add("classification", listClassValues);
            CreateKeysRequest.Key requestKey = new CreateKeysRequest.Key("reference_key", 2, dirAttributes);
            request.Keys.Add(requestKey);
            // Now ask the server to make those keys:
            CreateKeysResponse response;

            try
            {
                response = agent.CreateKeys(request);
            }
            catch (SdkException e)
            {
                System.Console.WriteLine("Error creating keys: {0}", e.Message);
                return(-1);
            }

            // Show us what keys we got (you can always get a key right when you create it):
            List <CreateKeysResponse.Key> responseKeys = response.Keys;
            GetKeysRequest fetchRequest = new GetKeysRequest(); //we will use this to track the keys we want to fetch later

            foreach (CreateKeysResponse.Key responseKey in responseKeys)
            {
                System.Console.WriteLine("We created a key with the Key Tag: {0}", responseKey.Id);
                fetchRequest.KeyIds.Add(responseKey.Id);
            }

            // The rest of this program would typically happen at a different time,
            //  not right after creating the keys, but when you were going to access
            //  the data protected by those keys.

            // Now, using the Key Tags, ask the server for those keys again:
            // NOTE: We populated fetchRequest's list of keytags in the above loop.
            GetKeysResponse fetchResponse;

            try
            {
                fetchResponse = agent.GetKeys(fetchRequest);
            }
            catch (SdkException e)
            {
                System.Console.WriteLine("Error fetching keys: {0}", e.Message);
                return(-1);
            }
            // Show what we got access to after a request for keys:
            foreach (GetKeysResponse.Key responseKey in fetchResponse.Keys)
            {
                System.Console.WriteLine("We fetched a key with the Key Tag: {0}", responseKey.Id);
            }

            // Tell us if we got less keys when we fetched than we created.
            //  This would happen if policy didn't give us access to all the keys.
            if (fetchResponse.Keys.Count < fetchRequest.KeyIds.Count)
            {
                System.Console.Write("We didn't get given all of the requested keys.");
                return(-2);
            }

            System.Console.Read();
            return(0);
        }