public bool SetProtection(FileOptions options)
        {
            var handler = CreateFileHandler(options);
            ProtectionDescriptor descriptor;

            if (options.TemplatelId != null)
            {
                descriptor = new ProtectionDescriptor(options.TemplatelId);
            }
            else
            {
                descriptor = new ProtectionDescriptor(options.UserRoles);
                var myCustomAppGuid = System.Guid.NewGuid();
                Console.WriteLine("Using GUID: {0}", myCustomAppGuid.ToString());
                descriptor.EncryptedAppData = new Dictionary <string, string>();
                descriptor.EncryptedAppData.Add("customGuid", myCustomAppGuid.ToString());

                descriptor.SignedAppData = new Dictionary <string, string>();
                descriptor.SignedAppData.Add("customGuid", myCustomAppGuid.ToString());
            }

            handler.SetProtection(descriptor, new ProtectionSettings());
            var result = handler.CommitAsync(options.OutputName).Result;

            return(result);
        }
Ejemplo n.º 2
0
        // Create a handler for publishing.
        public IProtectionHandler CreatePublishingHandler(string templateId)
        {
            ProtectionDescriptor protectionDescriptor = new ProtectionDescriptor(templateId);
            PublishingSettings   publishingSettings   = new PublishingSettings(protectionDescriptor);

            var protectionHandler = engine.CreateProtectionHandlerForPublishing(publishingSettings);

            return(protectionHandler);
        }
        private static void RunTest(string descriptorString)
        {
            Console.WriteLine($"Trying descriptor '{descriptorString}'.");
            byte[] plaintextBlob = Encoding.UTF8.GetBytes("Hello!");
            using (var protectionDescriptor = new ProtectionDescriptor(descriptorString))
            {
                byte[] protectedBlob = protectionDescriptor.ProtectSecret(plaintextBlob);
                Console.WriteLine("ProtectSecret succeeded.");

                byte[] roundTrippedBlob   = ProtectionDescriptor.UnprotectSecret(protectedBlob);
                string roundTrippedString = Encoding.UTF8.GetString(roundTrippedBlob);
                if (roundTrippedString == "Hello!")
                {
                    Console.WriteLine("UnprotectSecret succeeded.");
                }
                else
                {
                    throw new CryptographicException($"UnprotectSecret failed: Expected 'Hello!' but got '{roundTrippedString}'!");
                }
            }
        }
Ejemplo n.º 4
0
    /// <summary>
    /// Set the label on the given file.
    /// Options for the labeling operation are provided in the FileOptions parameter.
    /// </summary>
    /// <param name="options">Details about file input, output, label to apply, etc.</param>
    /// <returns></returns>
    public bool SetLabel(FileOptions options)
    {
        // LabelingOptions allows us to set the metadata associated with the labeling operations.
        // Review the API Spec at https://aka.ms/mipsdkdocs for details
        LabelingOptions labelingOptions = new LabelingOptions()
        {
            AssignmentMethod = options.AssignmentMethod
        };

        var handler = CreateFileHandler(options);

        // Use the SetLabel method on the handler, providing label ID and LabelingOptions
        // The handler already references a file, so those details aren't needed.

        try
        {
            handler.SetLabel(engine.GetLabelById(options.LabelId), labelingOptions, new ProtectionSettings());
        }

        catch (Microsoft.InformationProtection.Exceptions.JustificationRequiredException)
        {
            Console.Write("Please provide justification: ");
            string justification = Console.ReadLine();

            labelingOptions.IsDowngradeJustified = true;
            labelingOptions.JustificationMessage = justification;

            handler.SetLabel(engine.GetLabelById(options.LabelId), labelingOptions, new ProtectionSettings());
        }

        catch (Microsoft.InformationProtection.Exceptions.AdhocProtectionRequiredException)
        {
            List <string> users = new List <string>()
            {
                "*****@*****.**",
                "*****@*****.**",
                "*****@*****.**"
            };

            List <string> roles = new List <string>()
            {
                Microsoft.InformationProtection.Protection.Roles.Viewer
            };

            List <UserRoles> userroles = new List <UserRoles>()
            {
                new UserRoles(users, roles)
            };

            ProtectionDescriptor protectionDescriptor = new ProtectionDescriptor(userroles);

            handler.SetProtection(protectionDescriptor, new ProtectionSettings());
            handler.SetLabel(engine.GetLabelById(options.LabelId), labelingOptions, new ProtectionSettings());
        }


        // The change isn't committed to the file referenced by the handler until CommitAsync() is called.
        // Pass the desired output file name in to the CommitAsync() function.
        var result = Task.Run(async() => await handler.CommitAsync(options.OutputName)).Result;

        // If the commit was successful and GenerateChangeAuditEvents is true, call NotifyCommitSuccessful()
        if (result && options.GenerateChangeAuditEvent)
        {
            // Submits and audit event about the labeling action to Azure Information Protection Analytics
            handler.NotifyCommitSuccessful(options.FileName);
        }

        return(result);
    }