コード例 #1
0
        public void TestGetKeys()
        {
            var client = new DefaultOneNETClient(url, appkey, "");
            //test get all
            var req = new GetKeysRequest();
            var rsp = client.Execute(req);

            Console.WriteLine(rsp.Body);
            Assert.IsFalse(rsp.IsError);
            Assert.IsNotNull(rsp.Data);
            Assert.IsTrue(rsp.Data.Total_Count > 0);

            //test get by key
            var c1   = new DefaultOneNETClient(url, appkey, "");
            var req1 = new GetKeysRequest {
                Key = "jb3idOcNvc3Tv=WtoBLBLPYgVyg="
            };
            var rsp1 = c1.Execute(req1);

            Assert.IsFalse(rsp1.IsError);
            Assert.IsNotNull(rsp1.Data);
            Assert.IsTrue(rsp1.Data.Total_Count > 0);

            //test get by device id
            var c2   = new DefaultOneNETClient(url, appkey, "");
            var req2 = new GetKeysRequest {
                DeviceId = "776941"
            };
            var rsp2 = c2.Execute(req2);

            Assert.IsFalse(rsp2.IsError);
            Assert.IsNotNull(rsp2.Data);
            Assert.IsTrue(rsp1.Data.Total_Count > 0);
        }
コード例 #2
0
ファイル: DirectKeys.cs プロジェクト: greyp-ionic/samples
        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);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: jorge-sandoval/samples
        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);
        }