Exemple #1
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.
            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);
        }
        /// <summary>
        /// Applies the specified label to the provided file or stream.
        /// Justification message may be required if downgrading or removing label.
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="outputStream"></param>
        /// <param name="fileName"></param>
        /// <param name="labelId"></param>
        /// <param name="justificationMessage"></param>
        /// <returns></returns>
        public bool ApplyLabel(Stream stream, Stream outputStream, string fileName, string labelId, string justificationMessage)
        {
            IFileHandler handler;

            try
            {
                // Try to create an IFileHandler using private CreateFileHandler().
                if (stream != null)
                {
                    handler = CreateFileHandler(stream, fileName);
                }

                // Try to create an IFileHandler using private CreateFileHandler().
                else
                {
                    handler = CreateFileHandler(null, fileName);
                }

                // Applying a label requires LabelingOptions. Hard coded values here, but could be provided by user.
                LabelingOptions labelingOptions = new LabelingOptions()
                {
                    JustificationMessage = justificationMessage,
                    ActionSource         = ActionSource.Manual,
                    AssignmentMethod     = AssignmentMethod.Standard,
                    ExtendedProperties   = new List <KeyValuePair <string, string> >()
                };

                // Set the label on the input stream or file.
                handler.SetLabel(labelId, labelingOptions);

                // Call CommitAsync to write result to output stream.
                // Returns a bool to indicate true or false.
                var result = Task.Run(async() => await handler.CommitAsync(outputStream)).Result;

                if (result)
                {
                    // Submit an audit event if the change was successful.
                    handler.NotifyCommitSuccessful(fileName);
                }


                return(result);
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <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 DeleteLabel(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.DeleteLabel(labelingOptions);
            }

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

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

                handler.DeleteLabel(labelingOptions);
            }


            // 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);
        }
Exemple #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);
    }
        public ActionResult InvokeMIP()
        {
            string          name      = User.Identity.Name;
            ClaimsPrincipal principal = ClaimsPrincipal.Current;
            var             tenantId  = principal.FindFirst(c => c.Type == "http://schemas.microsoft.com/identity/claims/tenantid").Value;

            // Set path to bins folder.
            var path = Path.Combine(
                Directory.GetParent(Path.GetDirectoryName(new Uri(System.Reflection.Assembly.GetExecutingAssembly().CodeBase).LocalPath)).FullName,
                Environment.Is64BitProcess ? "bin\\x64" : "bin\\x86");

            //Initialize Wrapper for File API operations
            MIP.Initialize(MipComponent.File, path);

            //Create ApplicationInfo, setting the clientID from Azure AD App Registration as the ApplicationId
            ApplicationInfo appInfo = new ApplicationInfo()
            {
                ApplicationId      = clientId,
                ApplicationName    = appName,
                ApplicationVersion = appVersion
            };

            //Instatiate the AuthDelegateImpl object, passing in AppInfo.
            AuthDelegateImplementation authDelegate = new AuthDelegateImplementation(appInfo, tenantId);

            //Initialize and instantiate the File Profile
            //Create the FileProfileSettings object
            var profileSettings = new FileProfileSettings(mipPath, false, authDelegate, new ConsentDelegateImplementation(), appInfo, LogLevel.Trace);

            //Load the Profile async and wait for the result
            var fileProfile = Task.Run(async() => await MIP.LoadFileProfileAsync(profileSettings)).Result;

            //Create a FileEngineSettings object, then use that to add an engine to the profile
            var engineSettings = new FileEngineSettings(name, "", "en-US");

            engineSettings.Identity = new Identity(name);

            var fileEngine = Task.Run(async() => await fileProfile.AddEngineAsync(engineSettings)).Result;

            // Just a test code to check if all the custom labels are populated or not
            foreach (var label in fileEngine.SensitivityLabels)
            {
                string labelName = label.Name;
            }

            //Set paths and label ID. You can paas file input stream too
            string filePath       = Server.MapPath(@"~/App_Data/DemoAIPDoc.docx");
            string outputFilePath = @"D:\Test2.docx";

            var stream = System.IO.File.OpenRead(filePath);

            //Create a file handler for that file
            //Note: the 2nd inputFilePath is used to provide a human-readable content identifier for admin auditing.
            var handler = Task.Run(async() => await fileEngine.CreateFileHandlerAsync(stream, filePath, true)).Result;

            LabelingOptions labelingOptions = new LabelingOptions()
            {
                AssignmentMethod     = AssignmentMethod.Privileged, //important as we are removing the label
                IsDowngradeJustified = true,
                JustificationMessage = "Lowering label"
            };

            //remove the label
            handler.DeleteLabel(labelingOptions);

            // Commit changes, save as outputFilePath.  You can also generate output stream
            var result = Task.Run(async() => await handler.CommitAsync(outputFilePath)).Result;

            return(RedirectToAction("Index"));
        }