/// <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, AssignmentMethod = AssignmentMethod.Standard, ExtendedProperties = new List <KeyValuePair <string, string> >() }; // Set the label on the input stream or file. handler.SetLabel(fileEngine.GetLabelById(labelId), labelingOptions, new ProtectionSettings()); // 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 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); }